The release number of this article has been CHS307283
For Microsoft Visual Basic .NET versions of this article, see
305079.
For Microsoft Visual C .NET versions of this article, see
307402.
This task content
summary
Steps to create a SQL Server database
SUMMARY Programmakers often need to create a database in programming. This article describes how to create a Microsoft SQL Server database using ADO.NET and Visual C # .NET.
Back to top
Steps to create a SQL Server database
New Visual C # .NET Windows application. Place a button on Form1. Change the Name property of the button to BTNCREATEDATABASE to change the Text property to Create Database. Use the USING statement for System and System.Data namespace, so that you don't need to limit these namespaces in your code. Add the following code to the "General Declarations" section of Form1: use system;
Using system.data.sqlclient; switched back to Forms view, then double-click Create Database to add a Click event handler. Add the following code to the handler: String STR;
SqlConnection 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: //mydatabaseelog.ldf',"
"SIZE = 1MB,"
"Maxsize = 5MB,"
"FileGrowth = 10%)";
Sqlcommand mycommand = new sqlcommand (str, myconn);
Try
{
Myconn.open ();
mycommand.executenonquery ();
MessageBox.Show ("Database IS Created Success," MyProgram ", MessageBoxButtons.ok, MessageBoxicon.information;
}
Catch (System.exception EX)
{
MessageBox.show (ex. Tnowtring (), "MyProgram", MessageBoxButtons.ok, MessageBoxicon.information;
}
Finally
{
IF (MyConn.State == ConnectionState.Open)
{
MyConn.close ();
}
} Change the connection string to point to your SQL Server computer, and make sure the Database parameter is set to Master or empty. Press F5 or Ctrl F5 to run the project, and then click Create Database. Use the Server Explorer to verify the database creation.
Back to top
Remarks:
This code creates a custom database with specific properties. Before running the code, the folder of the created .mdf and .ldf file must already exist, it will generate an exception. If you want to create a database similar to the Model database similar to SQL Server and want it to be stored in the default location, then change the Str variable in the code: str = "Create Database MyDatabase"
Reference
For additional information for the CREATE TRANSACT-SQL command, see "SQL Server Book" or "MSDN Online Database":
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_create_1up1.asp For more information on ADO.NET objects and syntax, see "Microsoft .NET Framework SDK The following topics in the document "or" MSDN Online Database ":
Use ADO.NET access data http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingDatawithadonet.asp
Back to top
The information in this article applies to:
Microsoft ADO .NET (included in .NET Framework) Microsoft Visual C # .net (2002)
Recent Updated: 2002-2-21 (1.0) Keyword KBDSupport Kbhowto KbhowTomaster KB307283