Click here to Skip to main content
13,247,704 members (35,255 online)
Click here to Skip to main content
Add your own
alternative version


106 bookmarked
Posted 25 Apr 2005

Create an SQL Server Database Using C#

, 24 Apr 2013
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.


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);
         MessageBox.Show("Database has been created successfully!", 
                           "Create Database", MessageBoxButtons.OK, 
     catch (System.Exception ex)
         MessageBox.Show(ex.ToString(), "Create Database", 


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.


Any comments or questions can be sent to:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

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

You may also be interested in...


Comments and Discussions

GeneralRe: Actual Observation Pin
Nguyen Thanh Phuong26-Apr-05 18:25
memberNguyen Thanh Phuong26-Apr-05 18:25 
GeneralWTF Pin
Anonymous26-Apr-05 5:08
sussAnonymous26-Apr-05 5:08 
GeneralRe: WTF Pin
Anonymous26-Apr-05 14:53
sussAnonymous26-Apr-05 14:53 
GeneralRe: WTF Pin
Nguyen Thanh Phuong26-Apr-05 18:27
memberNguyen Thanh Phuong26-Apr-05 18:27 
Well, that's a question.
What will you do if you want to grant the right to create database for end user (people who use your software)?

GeneralRe: WTF Pin
hadjiyvanov27-Apr-05 1:02
memberhadjiyvanov27-Apr-05 1:02 
GeneralRe: WTF Pin
Member 31361519-Aug-09 11:42
memberMember 31361519-Aug-09 11:42 
GeneralRe: WTF Pin
Member 471095515-Oct-09 16:09
memberMember 471095515-Oct-09 16: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.

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