Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 307283 - Last Review: May 13, 2007 - Revision: 2.4
How To Create a SQL Server Database Programmatically by Using ADO.NET and Visual C# .NET
This article was previously published under Q307283
On This Page
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.
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 ("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%)";
SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "MyProgram", 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.
NOTES:
- This code creates a custom database with specific properties.
- 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.
- 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 = "CREATE DATABASE MyDatabase"
REFERENCES
For additional information about the
Create Database Transact-SQL command, see the SQL Server Books Online or visit the MSDN Online Library:
For more information about ADO.NET objects and syntax, see the following topic in the Microsoft .NET Framework SDK Documentation or visit the following Microsoft Web site:
APPLIES TO
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft ADO.NET 1.1
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
| kbhowtomaster kbsqlclient kbsystemdata KB307283 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please