Click here to Skip to main content
Licence 
First Posted 25 Apr 2005
Views 166,613
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
GeneralCoding for taking Sql Server Database Backup using C#.NET Pinmembersandeepgulecha3:25 20 Apr '06  
GeneralPerfect PinsussAhmed Erarslan21:56 1 Jun '05  
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  
GeneralRe: WTF PinsussAnonymous13:53 26 Apr '05  
You are suck if your client using msde.
GeneralRe: WTF PinmemberNguyen Thanh Phuong17:27 26 Apr '05  
GeneralRe: WTF Pinmemberhadjiyvanov0:02 27 Apr '05  
GeneralRe: WTF PinmemberMember 31361510:42 19 Aug '09  
GeneralRe: WTF PinmemberMember 471095515:09 15 Oct '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.

Permalink | Advertise | Privacy | Mobile
Web02 | 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