Guys anyone knows how to create tables at runtime? Or if it is a long story, kindly direct me to a site where I can learn more...

6 Years
Discussion Span
Last Post by markdean.expres

Not a long story.
1) Create a connection
2) Create a command containing the same SQL you would use to create it manually
3) cmd.ExecuteNonQuery()

Imports System.Data.SqlClient
Module Module1
   Sub Main()
      Dim strSQL As String = "CREATE TABLE GRP_CONFIG ( " + _
         "SHORT_NAME VARCHAR(8) NOT NULL " + _
         "TG_NAME VARCHAR(8) NOT NULL )"

      Dim csb As New SqlConnectionStringBuilder("connection info goes here")
      Dim conn As New SqlConnection(csb.ToString())
      Dim cmd As New SqlCommand(strSQL, conn)
   End Sub

End Module

Edited by thines01: added code


say I want to create a table upon clicking a button. Will I insert the whole thing within the sub of the button including the 'Imports System.Data.SqlClient'? And there is a module there. Don't really know where to put things here...


I would put that in a different Sub -- actually I would make it a function that returns a Boolean (false if table not created) and then call it from the button push.
I would not put all of that code IN the button-push itself.

As a matter of fact, if you're doing a lot of database interaction, you could make a new class of database related code and put it there.


I forget to tell you, I am using Access Database. I want to create table under the database file named Databank.


Yes, that's not a problem.
You will need to use the ODBC or OleDB classes to query the database.
Are you familiar with either?


I have just heard of them and in my Myapp.config file, there are computer-generated codes there where I saw something like it. So how would the code look like? Can you just repost it here? The table creation takes place when a button is clicked.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.