![]() |
General Programming »
Programming Tips »
General
Beginner
License: The Code Project Open License (CPOL)
Create a SQL Database ProgrammaticallyBy MoustafarIn this article, I will explain how to use Microsoft SQL Management Objects (SMO) to create a Microsoft SQL database from C# code. |
C# (C# 1.0, C# 2.0, C# 3.0), VB (VB 9.0), SQL, Windows (Win2K, WinXP, Win2003, Vista), Win Mobile, .NET (.NET 2.0, .NET 3.0, .NET 3.5), SQL Server (SQL 2005), Architect, DBA, Dev, QA, Design
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
In this article, I will explain how to use Microsoft SQL Management Objects (SMO) to create a Microsoft SQL database from C# code. In this sample, I will:
SQL Server Management Objects (SMO) are objects designed for programmatic management of Microsoft SQL Server. You can use SMO to build customized SQL Server management applications. Although SQL Server Management Studio is a powerful and extensive application for managing SQL Server, there might be times when you would be better served by an SMO application.
The SmoApplication.EnumAvailableSqlServers() enumerates a list of available instances of Microsoft SQL Server. It returns a DataTable object value that contains an enumerated list of information about the available instances of SQL Server. The table describes the different columns of the returned DataTable.
| Column | Data type | Description |
Name |
String |
The name of the instance of SQL Server. |
Server |
String |
The name of the server on which the instance of SQL Server is installed. |
Instance |
String |
The instance of SQL Server. |
IsClustered |
Boolean |
A Boolean value that is true if the instance is participating in failover clustering, or false if it is not. |
Version |
String |
The version of the instance of SQL Server. |
IsLocal |
Boolean |
A Boolean value that is true if the instance is local, or false if the instance is remote. |
DataTable dt = SmoApplication.EnumAvailableSqlServers();
foreach (DataRow dr in dt.Rows)
{
this.cbServers.Items.Add(dr[0]);
}
First we check if the user selected the local server or a SQL server:
//Connect to the local, default instance of SQL Server.
string srvname = this.cbServers.SelectedItem as string;
Server srv;
if (srvname == null)
{
srv = new Server();
sb.AppendLine("Connected to local SQL server");
}
else
{
srv = new Server(srvname);
sb.AppendLine(string.Format("Connected to {0}", srvname));
}
It is easy to check if the database already exists or not, just use the Databases attribute of the server as follows:
//Define a Database object variable by supplying the server and the
//database name arguments in the constructor.
Database db = srv.Databases[this.tbDBName.Text.Trim()];
if (db != null)
{
if (MessageBox.Show(string.Format
("The '{0}' already exists do you want to drop it?", this.tbDBName.Text),
"Warning", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)else
{
if (MessageBox.Show(string.Format
("Create the Tables and Stored Procedures for BT Error Manager on '{0}'?",
this.tbDBName.Text), "Warning", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
b.AppendLine("Creating the Tables and Stored Procedures.");
this.tbProgress.Text = sb.ToString();
db.ExecuteNonQuery(dbstring);
sb.AppendLine(string.Format
("Created the Tables and Stored Procedures for BT Error Manager on '{0}'",
this.tbDBName.Text));this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();"Proceed or select another database");
this.tbProgress.Text = sb.ToString();this.tbProgress.ScrollToCaret();
return;
To create a database, all you have to do is create a new Database object with the Server and the name of the database, then call the Create method.
db = new Database(srv, this.tbDBName.Text);
this.tbProgress.Text = sb.ToString();
this.tbProgress.ScrollToCaret();
//Create the database on the instance of SQL Server.
db.Create();
sb.AppendLine("Created the database.");
sb.AppendLine("Creating the Tables and Stored Procedures.");
this.tbProgress.Text = sb.ToString();this.tbProgress.ScrollToCaret();
The last step is to run the script that will create the tables, views, stored procedures, etc.
//'Reference the database and display the date when it was created.
db.ExecuteNonQuery(dbstring);
If you want to use this code in your project, you will need to add references to the SMO assemblies. You can locate the SMO assemblies in the C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies folder. Select the following files:
}
sb.AppendLine(
}
}
{
db.Drop();
}
Visit www.MoustafaRefaat.com to find more information.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Oct 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Moustafar Everything else Copyright © CodeProject, 1999-2009 Staging | Advertise on the Code Project |