Click here to Skip to main content
15,881,248 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 413.5K   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

 
SuggestionBetter ways of doing this Pin
jim lahey24-Apr-13 4:56
jim lahey24-Apr-13 4:56 
GeneralRe: Better ways of doing this Pin
Thomas Haller25-Apr-13 1:32
Thomas Haller25-Apr-13 1:32 
QuestionFolder in Program Folder Pin
cipristupi17-Jun-12 8:22
cipristupi17-Jun-12 8:22 
GeneralUpdate to use the program on network and some problems Pin
Egypt_Nemo26-Mar-12 17:56
Egypt_Nemo26-Mar-12 17:56 
GeneralMy vote of 4 Pin
Niyazi Yarar6-Feb-12 0:03
Niyazi Yarar6-Feb-12 0:03 
NewsConnection string!!! [modified] Pin
Tomasz Karlinski17-May-11 9:40
Tomasz Karlinski17-May-11 9:40 
Generaldont work Pin
priazma20-Mar-11 3:42
priazma20-Mar-11 3:42 
NewsMy vote of 1 Pin
Eddy Vluggen24-Apr-13 4:54
professionalEddy Vluggen24-Apr-13 4:54 
GeneralFreezing and not working. Pin
Serkan111-Jan-11 14:07
Serkan111-Jan-11 14:07 
GeneralRe: Freezing and not working. Pin
Goransol16-Mar-11 12:02
Goransol16-Mar-11 12:02 
GeneralRe: Freezing and not working. Pin
Eddy Vluggen24-Apr-13 4:53
professionalEddy Vluggen24-Apr-13 4:53 
GeneralRe: Freezing and not working. Pin
sw2437618-Nov-14 11:55
sw2437618-Nov-14 11:55 
GeneralMy vote of 4 Pin
aditya1989725-Dec-10 19:53
aditya1989725-Dec-10 19:53 
GeneralVery Nice Pin
Jaffar Ramnad14-Dec-10 23:12
Jaffar Ramnad14-Dec-10 23:12 
GeneralMy vote of 2 Pin
jojokim21-Oct-10 7:29
jojokim21-Oct-10 7:29 
GeneralNice Pin
rain_119-Aug-10 23:09
rain_119-Aug-10 23:09 
GeneralRe: Nice Pin
Groulien22-Mar-11 3:39
Groulien22-Mar-11 3:39 
(Late, I know)
Try starting your app/visual studio as administrator and try again.
GeneralNice one Pin
valeranavin2-May-10 20:33
valeranavin2-May-10 20:33 
Generalthank you Pin
Rolf Böhlke26-Apr-10 10:54
Rolf Böhlke26-Apr-10 10:54 
GeneralGood article Pin
Donsw19-Jan-09 9:24
Donsw19-Jan-09 9:24 
GeneralRe: Good article Pin
Member 31361519-Aug-09 10:39
Member 31361519-Aug-09 10:39 
GeneralHelp.. Pin
BicycleTheif12-Nov-08 20:00
BicycleTheif12-Nov-08 20:00 
General5 Stars Pin
Derek Bartram16-Apr-08 2:57
Derek Bartram16-Apr-08 2:57 
QuestionHow to solve the error says An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. That is occur when i need to execute the stored procedure using C# Pin
Shiv524-May-06 19:58
Shiv524-May-06 19:58 
AnswerRe: How to solve the error says An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. That is occur when i need to execute the stored procedure using C# Pin
ardaolca14-Jan-09 1:06
ardaolca14-Jan-09 1:06 

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.