Click here to Skip to main content
15,868,306 members
Articles / Database Development / SQL Server

Create an SQL Server Database Using C#

Rate me:
Please Sign up or sign in to vote.
3.58/5 (41 votes)
24 Apr 2013CPOL 413K   19.6K   108   36
Create a SQL Server database using C#.

Image 1

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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Vietnam Vietnam
I am Vietnamese. I am a researcher in Computer Science and a freelance translator in German and Vietnamese.
Willing to share experience!

Comments and Discussions

 
GeneralCoding for taking Sql Server Database Backup using C#.NET Pin
sandeepgulecha20-Apr-06 3:25
sandeepgulecha20-Apr-06 3:25 
GeneralPerfect Pin
Ahmed Erarslan1-Jun-05 21:56
Ahmed Erarslan1-Jun-05 21:56 
GeneralRe: Perfect Pin
Ahmed Erarslan1-Jun-05 21:57
Ahmed Erarslan1-Jun-05 21:57 
GeneralActual Observation Pin
Anonymous26-Apr-05 15:37
Anonymous26-Apr-05 15:37 
GeneralRe: Actual Observation Pin
Phuong Thanh Nguyen26-Apr-05 17:25
Phuong Thanh Nguyen26-Apr-05 17:25 
GeneralWTF Pin
Anonymous26-Apr-05 4:08
Anonymous26-Apr-05 4:08 
GeneralRe: WTF Pin
Anonymous26-Apr-05 13:53
Anonymous26-Apr-05 13:53 
GeneralRe: WTF Pin
Phuong Thanh Nguyen26-Apr-05 17:27
Phuong Thanh Nguyen26-Apr-05 17:27 
GeneralRe: WTF Pin
hadjiyvanov27-Apr-05 0:02
hadjiyvanov27-Apr-05 0:02 
GeneralRe: WTF Pin
Member 31361519-Aug-09 10:42
Member 31361519-Aug-09 10:42 
GeneralRe: WTF Pin
Member 471095515-Oct-09 15:09
Member 471095515-Oct-09 15:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.