HOW TO: Create a SQL Server Database Programmatically by Using ADO.NET and Visual J# .NET (320628)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework) 1.0
- Microsoft ADO.NET (included with the .NET Framework 1.1)
- Microsoft Visual J# .NET (2002)
- Microsoft Visual J# .NET (2003)
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 TASKSUMMARY 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("Server=localhost;" +
"Integrated security=SSPI;" +
"database=master");
str = "CREATE DATABASE MyDatabase ON PRIMARY" +
" (NAME = MyDatabase_Data," +
" FILENAME = 'C:\\MyDatabaseData.mdf'," +
" SIZE = 2MB," +
" MAXSIZE = 10MB," +
" FILEGROWTH = 10%)" +
" LOG ON" +
" (NAME = MyDatabase_Log," +
" FILENAME = 'C:\\MyDatabaseLog.ldf'," +
" SIZE = 1MB," +
" MAXSIZE = 5MB," +
" FILEGROWTH = 10%)";
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("Database is created successfully",
"MyProgram", 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.
back to the top
Additional Notes
back to the top
REFERENCES For more information about the CREATE DATABASE Transact-SQL
command, see the SQL Server Books Online or MSDN Online Library: For more information about ADO.NET objects and syntax, see the
Microsoft .NET Framework SDK 1.0 Documentation or MSDN Online Library: For more general information about ADO.NET, visit the following
MSDN newsgroup: For more information about Visual J# .NET, visit the following
MSDN newsgroup:
back to the top
Modification Type: | Major | Last Reviewed: | 7/24/2006 |
---|
Keywords: | kbHOWTOmaster KB320628 kbAudDeveloper |
---|
|