Microsoft KB Archive/305079

= How to create a SQL Server database programmatically by using ADO.NET and Visual Basic .NET =

Article ID: 305079

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q305079





For a Microsoft Visual C++ .NET version of this article, see 307402.



For a Microsoft Visual C# .NET version of this article, see 307283.



For a Microsoft Visual J# .NET version of this article, see 320628.

IN THIS TASK
SUMMARY
 * Steps to Create the Sample
 * Additional Notes

REFERENCES



SUMMARY
Programmers often need to create databases programmatically. This article describes how to use ADO.NET and Visual Basic .NET to programmatically create a Microsoft SQL Server database.

back to the top

Steps to create the sample
 Create a new Visual Basic .NET Windows Application project. Form1 is added to the project by default. Place a Command button on Form1, and change its Name property to btnCreateDatabase and its Text property to Create Database.  Copy and paste the following line of code into Form1's &quot;general declaration&quot; section: Imports System.Data.SqlClient   Copy and paste the following code after the region &quot;Windows Form Designer generated code&quot;: Private Sub btnCreateDatabase_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCreateDatabase.Click Dim str As String

Dim myConn As SqlConnection = New SqlConnection(&quot;Server=(local)\netsdk;&quot; & _                                                   &quot;uid=sa;pwd=;database=master&quot;)

str = &quot;CREATE DATABASE MyDatabase ON PRIMARY &quot; & _ &quot;(NAME = MyDatabase_Data, &quot; & _         &quot; FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', &quot; & _          &quot; SIZE = 2MB, &quot; & _          &quot; MAXSIZE = 10MB, &quot; & _          &quot; FILEGROWTH = 10%) &quot; & _ &quot; LOG ON &quot; & _ &quot;(NAME = MyDatabase_Log, &quot; & _         &quot; FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', &quot; & _          &quot; SIZE = 1MB, &quot; & _          &quot; MAXSIZE = 5MB, &quot; & _          &quot; FILEGROWTH = 10%) &quot;

Dim myCommand As SqlCommand = New SqlCommand(str, myConn)

Try myConn.Open myCommand.ExecuteNonQuery MessageBox.Show(&quot;Database is created successfully&quot;, _                       &quot;MyProgram&quot;, MessageBoxButtons.OK, _                         MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.ToString) Finally If (myConn.State = ConnectionState.Open) Then myConn.Close End If      End Try

End Sub  Change the connection string to point to your SQL Server, and make sure that the Database argument is set to Master or blank.</li> Press F5 or CTRL+F5 to run the project, and then click Create Database.</li></ol>

back to the top

Additional notes
<ul> This code creates a custom database with specific properties.</li> The folder that will hold the created .mdf and .ldf files must already exist before you run the code or an exception will be generated.</li>  If you want to create a database that is similar to SQL Server's Model database and in the default location, then change the str variable in the code: str = &quot;CREATE DATABASE MyDatabase&quot; </li></ul>

back to the top

<div class="references_section">