Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Create SQL Server database and table using Visual Basic.Net.

Author: Shahzad

It's very easy to create an SQL Server database and tables using VB.NET. This article uses System.Data.SqlClient namespace to do this.

First of all open a connection to a SQL Server using your own server name and login information.

Dim objConn As New SqlConnection("Server=<servername>;uid=<userid>;pwd=<password>;database=master")

objConn.Open()

Now,  you can execute a command to create a database. Although, you can specify all the database attributes like File Sizes, File Locations, and Growth Size etc, however if you don't specify these attributes SQL Server will provide the default settings.

Dim objCmd As New SqlCommand("Create Database TestDB", objConn)

objCmd.CommandType = CommandType.Text

objCmd.ExecuteNonQuery()

Using above statements we have created a TestDB database. Now we are ready to create table in this database.

Open connection to this database first.

Dim objConn As New SqlConnection("Server=<servername>;uid=<userid>;pwd=<password>;database=TestDB")

objConn.Open()

Then, add the following code to create a table the opened database.

Dim objCmd As New SqlCommand("Create Table TestTable(ID Int,Name Varchar(50))", objConn)

objCmd.CommandType = CommandType.Text

objCmd.ExecuteNonQuery()

 

Article Comments
yes it fit my doubt ,
thank you

Posted on 1/8/2007 5:11:13 AM by chandrapraksh

Hi man this really helped me a lot man thanks for your service you just made it so simple and easy to understand.

Posted on 1/23/2007 7:52:50 AM by Maxx

Hi man, i see a lot of this things in the web, but yours is really simple and easy to follow, thanks

Posted on 5/21/2008 8:35:20 AM by Armando

Dear Sir,
Where i will write this code above code

Posted on 9/28/2008 4:31:03 AM by farhan

Thnax a lot it was very useful to me. :)

Posted on 3/11/2009 5:20:09 AM by sha

how to create a table in vb.net using a textbox..

Posted on 5/26/2009 12:00:37 AM by zatul

this help me a lot and very useful for me :D

Posted on 1/19/2010 9:27:25 PM by zhihow

is it possible to provide a working example to download.

Posted on 5/30/2010 5:46:08 AM by wellyn

I needed to add this in my global declaration:
Imports System.Data.SqlClient

Posted on 6/30/2010 12:13:11 PM by Addon

really its very simple way,,,,
thank's alot for help, I will try it now.

Posted on 10/29/2010 3:23:51 PM by Sameh

how I can specify the database File Locations?

Posted on 11/18/2010 4:52:31 AM by Sameh

Can u please tel me the code to verify whether the table has been created or not?

Posted on 12/2/2010 12:43:14 AM by prawin

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Basic Registry manipulation in VB.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net