Microsoft KB Archive/307283

= How To Create a SQL Server Database Programmatically by Using ADO.NET and Visual C# .NET =

Article ID: 307283

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q307283



For a Microsoft Visual Basic .NET version of this article, see 305079.

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

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

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Steps to Create a SQL Server Database

REFERENCES



SUMMARY
This step-by-step article shows you how to create a Microsoft SQL Server database by using ADO.NET and Visual C# .NET because programmers often need to create Databases programmatically.

back to the top

Steps to Create a SQL Server Database
To create the database, follow these steps:  Create a new Visual C# .NET Windows application. Place a button on Form1. Change the button's Name property to btnCreateDatabase, and then change the Text property to Create Database .  Use the using statement on the System and System.Data namespaces so that you do not have to qualify declarations in those namespaces later in your code. Add the following code to the General Declarations section of Form1: using System; using System.Data.SqlClient;   Switch to Form view, and then double-click Create Database to add the click event handler. Add the following sample code to the handler: String str; SqlConnection myConn = new SqlConnection (&quot;Server=localhost;Integrated security=SSPI;database=master&quot;);

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

SqlCommand myCommand = 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 (System.Exception ex) {   MessageBox.Show(ex.ToString, &quot;MyProgram&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information); }   finally {   if (myConn.State == ConnectionState.Open) {       myConn.Close; }   }                     Change the connection string to point to your computer running SQL Server, and then verify that the Database argument is set to Master or is blank. Press F5 or CTRL+F5 to run the project, and then click Create Database. Use the Server Explorer to verify that the database is created.</li></ol>

back to the top

NOTES:
<ul> This code creates a custom database with specific properties.</li> The folder that is going to 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 the SQL Server Model database, and you want the database in the default location, then change the str variable in the code, as in the following sample code: str = &quot;CREATE DATABASE MyDatabase&quot; </li></ul>

back to the top

<div class="references_section">