Click here to Skip to main content
Licence 
First Posted 25 Apr 2005
Views 166,654
Bookmarked 82 times

Create a SQL Server Database Using C#

By | 25 Apr 2005 | Article
Create a SQL Server database using C#.
 
Part of The SQL Zone sponsored by
See Also

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

Introduction

In this presentation, I would like to show you how to create a 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 SQL Server to manipulate the new database (master is the place 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

About the Author

Nguyen Thanh Phuong

Web Developer

Vietnam Vietnam

Member

I am Vietnamese. I am PhD Student in Germany. My study is Information Technology.
My interest is wiki & programming.
Willing to share experience!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUpdate to use the program on network and some problems PinmemberEgypt_Nemo17:56 26 Mar '12  
GeneralMy vote of 4 PinmemberNiyazi Yarar0:03 6 Feb '12  
NewsConnection string!!! [modified] PinmemberTomasz Karlinski9:40 17 May '11  
Generaldont work Pinmemberpriazma3:42 20 Mar '11  
GeneralFreezing and not working. PinmemberSerkan114:07 11 Jan '11  
GeneralRe: Freezing and not working. PinmemberGoransol12:02 16 Mar '11  
GeneralMy vote of 4 Pinmemberaditya1989719:53 25 Dec '10  
GeneralVery Nice Pinmemberjaffar Ramnad23:12 14 Dec '10  
GeneralMy vote of 2 Pinmemberjojokim7:29 21 Oct '10  
GeneralNice Pinmemberrain_123:09 19 Aug '10  
GeneralRe: Nice Pinmembernbgangsta3:39 22 Mar '11  
GeneralNice one Pinmembervaleranavin20:33 2 May '10  
Generalthank you PinmemberRolf Böhlke10:54 26 Apr '10  
GeneralGood article PinmemberDonsw9:24 19 Jan '09  
GeneralRe: Good article PinmemberMember 31361510:39 19 Aug '09  
GeneralHelp.. PinmemberNithya..20:00 12 Nov '08  
General5 Stars PinmemberDerek Bartram2:57 16 Apr '08  
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# PinmemberShiv519:58 24 May '06  
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# Pinmemberardaolca1:06 14 Jan '09  
GeneralCoding for taking Sql Server Database Backup using C#.NET Pinmembersandeepgulecha3:25 20 Apr '06  
GeneralPerfect PinsussAhmed Erarslan21:56 1 Jun '05  
Conratulation This Article Perfect Wonderful
 
Wink | ;) Cool | :cool:
 
Ahmed Erarslan
MCAD,MCDBA,MCP

MCSD.NET
GeneralRe: Perfect PinmemberAhmad(Ahmed)21:57 1 Jun '05  
GeneralActual Observation PinsussAnonymous15:37 26 Apr '05  
GeneralRe: Actual Observation PinmemberNguyen Thanh Phuong17:25 26 Apr '05  
GeneralWTF PinsussAnonymous4:08 26 Apr '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 26 Apr 2005
Article Copyright 2005 by Nguyen Thanh Phuong
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid