Click here to Skip to main content
11,413,548 members (72,270 online)
Click here to Skip to main content

Create an SQL Server Database Using C#

, 24 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nguyen Thanh Phuong
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!
Follow on   LinkedIn

Comments and Discussions

 
SuggestionBetter ways of doing this Pin
jim lahey at 24-Apr-13 5:56
memberjim lahey24-Apr-13 5:56 
GeneralRe: Better ways of doing this Pin
Thomas Haller at 25-Apr-13 2:32
memberThomas Haller25-Apr-13 2:32 
QuestionFolder in Program Folder Pin
cipri94 at 17-Jun-12 9:22
membercipri9417-Jun-12 9:22 
GeneralUpdate to use the program on network and some problems Pin
Egypt_Nemo at 26-Mar-12 18:56
memberEgypt_Nemo26-Mar-12 18:56 
GeneralMy vote of 4 Pin
Niyazi Yarar at 6-Feb-12 1:03
memberNiyazi Yarar6-Feb-12 1:03 
NewsConnection string!!! [modified] Pin
Tomasz Karlinski at 17-May-11 10:40
memberTomasz Karlinski17-May-11 10:40 
Generaldont work Pin
priazma at 20-Mar-11 4:42
memberpriazma20-Mar-11 4:42 
NewsMy vote of 1 Pin
Eddy Vluggen at 24-Apr-13 5:54
mvpEddy Vluggen24-Apr-13 5:54 
GeneralFreezing and not working. Pin
Serkan1 at 11-Jan-11 15:07
memberSerkan111-Jan-11 15:07 
GeneralRe: Freezing and not working. Pin
Goransol at 16-Mar-11 13:02
memberGoransol16-Mar-11 13:02 
GeneralRe: Freezing and not working. Pin
Eddy Vluggen at 24-Apr-13 5:53
mvpEddy Vluggen24-Apr-13 5:53 
GeneralRe: Freezing and not working. Pin
sw24376 at 18-Nov-14 12:55
membersw2437618-Nov-14 12:55 
GeneralMy vote of 4 Pin
aditya19897 at 25-Dec-10 20:53
memberaditya1989725-Dec-10 20:53 
GeneralVery Nice Pin
jaffar Ramnad at 15-Dec-10 0:12
memberjaffar Ramnad15-Dec-10 0:12 
GeneralMy vote of 2 Pin
jojokim at 21-Oct-10 8:29
memberjojokim21-Oct-10 8:29 
GeneralNice Pin
rain_1 at 20-Aug-10 0:09
memberrain_120-Aug-10 0:09 
GeneralRe: Nice Pin
nbgangsta at 22-Mar-11 4:39
membernbgangsta22-Mar-11 4:39 
(Late, I know)
Try starting your app/visual studio as administrator and try again.
GeneralNice one Pin
valeranavin at 2-May-10 21:33
membervaleranavin2-May-10 21:33 
Generalthank you Pin
Rolf Böhlke at 26-Apr-10 11:54
memberRolf Böhlke26-Apr-10 11:54 
GeneralGood article Pin
Donsw at 19-Jan-09 10:24
memberDonsw19-Jan-09 10:24 
GeneralRe: Good article Pin
Member 313615 at 19-Aug-09 11:39
memberMember 31361519-Aug-09 11:39 
GeneralHelp.. Pin
Nithya.. at 12-Nov-08 21:00
memberNithya..12-Nov-08 21:00 
General5 Stars Pin
Derek Bartram at 16-Apr-08 3:57
memberDerek Bartram16-Apr-08 3: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
Shiv5 at 24-May-06 20:58
memberShiv524-May-06 20: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
ardaolca at 14-Jan-09 2:06
memberardaolca14-Jan-09 2:06 
GeneralCoding for taking Sql Server Database Backup using C#.NET Pin
sandeepgulecha at 20-Apr-06 4:25
membersandeepgulecha20-Apr-06 4:25 
GeneralPerfect Pin
Ahmed Erarslan at 1-Jun-05 22:56
sussAhmed Erarslan1-Jun-05 22:56 
GeneralRe: Perfect Pin
Ahmad(Ahmed) at 1-Jun-05 22:57
memberAhmad(Ahmed)1-Jun-05 22:57 
GeneralActual Observation Pin
Anonymous at 26-Apr-05 16:37
sussAnonymous26-Apr-05 16:37 
GeneralRe: Actual Observation Pin
Nguyen Thanh Phuong at 26-Apr-05 18:25
memberNguyen Thanh Phuong26-Apr-05 18:25 
GeneralWTF Pin
Anonymous at 26-Apr-05 5:08
sussAnonymous26-Apr-05 5:08 
GeneralRe: WTF Pin
Anonymous at 26-Apr-05 14:53
sussAnonymous26-Apr-05 14:53 
GeneralRe: WTF Pin
Nguyen Thanh Phuong at 26-Apr-05 18:27
memberNguyen Thanh Phuong26-Apr-05 18:27 
GeneralRe: WTF Pin
hadjiyvanov at 27-Apr-05 1:02
memberhadjiyvanov27-Apr-05 1:02 
GeneralRe: WTF Pin
Member 313615 at 19-Aug-09 11:42
memberMember 31361519-Aug-09 11:42 
GeneralRe: WTF Pin
Member 4710955 at 15-Oct-09 16:09
memberMember 471095515-Oct-09 16:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 24 Apr 2013
Article Copyright 2005 by Nguyen Thanh Phuong
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid