Click here to Skip to main content
13,042,307 members (92,845 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 5 Oct 2006

Create database in sql server using with C# (VS2005)

, 5 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Create database in sql server using with C# (VS2005)


It is a sample ASP.Net (VS2005) application for creating a database in Sql server Runtime. I have used SQL-DMO dll. This article will show you how to create a database in Sql server 2005 & 2000.

Some important functions are discussed below:

Add reference to SQL-DMO dll

You can do this by right clicking the project in Solution Explorer, then selecting 'Add Reference', COM components and the latest version of "Microsoft SQLDMO Object Library".

Available Server

SQLDMO.Application oSQLServerDMOApp = new SQLDMO.Application();

SQLDMO.NameList oNameList;

oNameList = oSQLServerDMOApp.ListAvailableSQLServers();

for (int intIndex = 0; intIndex <= oNameList.Count - 1; intIndex++)


if (oNameList.Item(intIndex as object) != null)









cboServers.SelectedIndex = 0;

Create a database on the server

string strDatabaseName = "";

SQLDMO.SQLServer gSQLServerDMO = new SQLDMO.SQLServer();

SQLDMO.Database nDatabase = new SQLDMO.Database();

SQLDMO.DBFile nDBFileData = new SQLDMO.DBFile();

SQLDMO.LogFile nLogFile = new SQLDMO.LogFile();

strDatabaseName = txt_Database.Text.ToString();



gSQLServerDMO.Connect(this.cboServers.SelectedItem.ToString(), this.txtUser.Text, this.txtPassword.Text);

nDatabase.Name = strDatabaseName;

nDBFileData.Name = strDatabaseName;

nDBFileData.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + strDatabaseName + "_Data.mdf";

nDBFileData.PrimaryFile = true;

nDBFileData.Size = 2;


nDBFileData.FileGrowth = 1;

//Add the DBFile object



nLogFile.Name = strDatabaseName + "Log";

nLogFile.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + strDatabaseName + "_Log.ldf";

nLogFile.Size = 2;



Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('Database Sucessfully Created');</script>");



catch (Exception SQLDBException)


Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");







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

Web Developer
India India
I'm Joshy George.I finished my Post Graduation in Computer Application,i'm from the great 'Gods Own Country', Kerala, India. Now I'm working in Infopark Kochin, as a specialised areas are C# ,Asp.Net ,Vb6,smart card application,Tapii,Comm ctrls.

You may also be interested in...


Comments and Discussions

Generalit works with some changes for SQL server 2008! Pin
sanchay10111-Feb-09 1:40
membersanchay10111-Feb-09 1:40 

The code did work for me.

I was using SQL server 2008 and for that min requirement for primary file is 3 MB. Thus change the size of dbphysical file from 2 to 3 (or more than 3).

Make sure the authentication is not windows authentication and the user has dbcreator and sysadmin rights.
Generalusing windows authentication Pin
jikubhai27-Sep-07 21:29
memberjikubhai27-Sep-07 21:29 
Generaldatabase need more... Pin
pk_mittal19-Nov-06 20:27
memberpk_mittal19-Nov-06 20:27 

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
Web02 | 2.8.170713.1 | Last Updated 5 Oct 2006
Article Copyright 2006 by Joshy_geo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid