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...
Thanks

Recommended Answers

All 8 Replies

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())
      '
      conn.Open()
      Dim cmd As New SqlCommand(strSQL, conn)
      cmd.ExecuteNonQuery()
      conn.Close()
      '
   End Sub

End Module

where will I insert this code? within a button or within the class?

Wherever you need it.
It depends on what triggers table creates.

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.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.