How to Create a Table in VB.NET
VB.NET uses ADO.NET controls to connect to a database server and run queries. The "create table" query creates a table from your VB.NET code. Programmers create tables from code to set up temporary tables, or to create installation files for a website template program. You use the SqlCommand control to run a query on the database server to create a new table.
Instructions
-
-
1
Click the Windows "Start" button and select All Programs. Click Microsoft .NET, then select the Visual Studio shortcut. Open your VB.NET project in VS.
-
2
Add the class libraries to the top of your code file. Copy and paste the following code to the VB.NET file you want to use to create the table:
Imports System.Data
Imports System.Data.SqlClient
-
-
3
Create the SQL connection control. The connection uses your user name and password to connect to the website's database. The following code creates your connection:
Dim connection As New SqlConnection
connection.ConnectionString = "Data Source=localhost;Initial Catalog=Customers;Persist Security Info=True;User ID=user;Password=2222"
The connection string in this example connects to the localhost server and initializes the Customers database. Use your own database information in the string to connect to the server.
-
4
Create the "create table" query. The following code sets up the SQL command to create a new table called mytemptable:
Dim command As New SqlCommand("create table mytemptable", connection)
Notice the command takes a parameter that includes the connection you created earlier. You need this connection control with each of your queries to tell VB.NET where to connect.
-
5
Execute the query to create the table. The following line of code creates the table on the database:
command.ExecuteNonQuery()
-
1