Microsoft KB Archive/320628

= HOW TO: Create a SQL Server Database Programmatically by Using ADO.NET and Visual J# .NET =

Article ID: 320628

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q320628





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 Basic .NET version of this article, see 305079.

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

REFERENCES



SUMMARY
Programmers must frequently create databases programmatically. This step-by-step article describes how to use ADO.NET and Visual J# to programmatically create a Microsoft SQL Server database.

back to the top

Steps to Create the Sample
 Create a new Visual J# Windows Application project. By default, Form1 is added to the project. Place a Command button on Form1, and then change its Name property to btnCreateDatabase and its Text property to Create Database.  Paste the following line of code below the other import statements in Form1.jsl: import System.Data.SqlClient.*;   Paste the following code after the Windows Form Designer generated code region: private void btnCreateDatabase_Click (System.Object sender, System.EventArgs e) { SqlDataReader reader; String str; SqlConnection myConn; SqlCommand myCommand; try {

myConn = new SqlConnection(&quot;Server=localhost;&quot; +                &quot;Integrated security=SSPI;&quot; +                 &quot;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,&quot; +    &quot; MAXSIZE = 10MB,&quot; +    &quot; FILEGROWTH = 10%)&quot; + &quot; LOG ON&quot; + &quot; (NAME = MyDatabase_Log,&quot; +   &quot; FILENAME = 'C:\\MyDatabaseLog.ldf',&quot; +    &quot; SIZE = 1MB,&quot; +    &quot; MAXSIZE = 5MB,&quot; +    &quot; FILEGROWTH = 10%)&quot;; myCommand= new SqlCommand(str, myConn);

myConn.Open; reader = myCommand.ExecuteReader; if (reader != null) {       reader.Close; if(myConn.get_State == ConnectionState.Open) {           myConn.Close; }               MessageBox.Show(&quot;Database is created successfully&quot;,          &quot;MyProgram&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information); }        }         catch(Exception ex) {       MessageBox.Show(ex.fillInStackTrace.toString); } }                    Change the connection string to point to the computer that is running SQL Server, and then make sure that the Database argument is either blank or is set to Master. 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 the SQL Server Model database and that is in the default location, change the str variable in the code to the following: str = &quot;CREATE DATABASE MyDatabase&quot; </li></ul>

back to the top

<div class="references_section">