Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create an SQL Server Database Using C#

0.00/5 (No votes)
24 Apr 2013 1  
Create a SQL Server database using C#.

This interface is akin to the SQL Server New Database menu.

Introduction

In this presentation, I would like to show you how to create an SQL Server database using C#. Actually I had to deal with the problem when programming for our own specific DBMS.

First of all, you have to create a connection to the master database of your SQL Server to manipulate the new database (master is the database where you can get details about the whole DBMS).

Using the code

The code for creating the database is very simple, the main function can be listed as follows:

private void CreateDatabase(DatabaseParam DBParam)
{
    System.Data.SqlClient.SqlConnection tmpConn;
    string sqlCreateDBQuery;
    tmpConn = new SqlConnection();
    tmpConn.ConnectionString = "SERVER = " + DBParam.ServerName + 
                         "; DATABASE = master; User ID = sa; Pwd = sa";
    sqlCreateDBQuery = " CREATE DATABASE "
                       + DBParam.DatabaseName
                       + " ON PRIMARY " 
                       + " (NAME = " + DBParam.DataFileName +", "
                       + " FILENAME = '" + DBParam.DataPathName +"', " 
                       + " SIZE = 2MB,"
                       + " FILEGROWTH =" + DBParam.DataFileGrowth +") "
                       + " LOG ON (NAME =" + DBParam.LogFileName +", "
                       + " FILENAME = '" + DBParam.LogPathName + "', " 
                       + " SIZE = 1MB, "
                       + " FILEGROWTH =" + DBParam.LogFileGrowth +") ";
     SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
     try
     {
         tmpConn.Open();
         MessageBox.Show(sqlCreateDBQuery);
         myCommand.ExecuteNonQuery();
         MessageBox.Show("Database has been created successfully!", 
                           "Create Database", MessageBoxButtons.OK, 
                                       MessageBoxIcon.Information);
      }
     catch (System.Exception ex)
     {
         MessageBox.Show(ex.ToString(), "Create Database", 
                                     MessageBoxButtons.OK, 
                              MessageBoxIcon.Information);
     }
     finally
     {
         tmpConn.Close();
     }
     return;
}

Note

You have to change the DBParam.ServerName to your appropriate SQL Server name.

Checking your results

To see the results, click on Enterprise Manager of SQL Server, click on the plus (+) next to your Server, click on (+) next to Database tab, and you can see the test DB.

Conclusion

Any comments or questions can be sent to: phamthuhai@gmail.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here