5,427,813 members and growing! (17,795 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

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

By Joshy_geo

Create database in sql server using asp.net with C# (VS2005)
SQL, C# 2.0, C#, Windows, .NET, Visual Studio, ASP.NET, SQL Server, SQL 2005, DBA, Dev

Posted: 5 Oct 2006
Updated: 5 Oct 2006
Views: 16,934
Bookmarked: 8 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.43 Rating: 3.00 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
1 vote, 33.3%
3
0 votes, 0.0%
4
1 vote, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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.Items.Add(oNameList.Item(intIndex).ToString());

}

else

{

cboServers.Items.Add("(Local)");

}

}

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();

try

{

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.FileGrowthType = SQLDMO.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB;

nDBFileData.FileGrowth = 1;

//Add the DBFile object

nDatabase.FileGroups.Item("PRIMARY").DBFiles.Add(nDBFileData);

//

nLogFile.Name = strDatabaseName + "Log";

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

nLogFile.Size = 2;

nDatabase.TransactionLog.LogFiles.Add(nLogFile);

gSQLServerDMO.Databases.Add(nDatabase);

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

gSQLServerDMO.DisConnect();

}

catch (Exception SQLDBException)

{

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

return;

}

finally

{

}

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

Joshy_geo


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 Sr.software Engineer.my specialised areas are C# ,Asp.Net ,Vb6,smart card application,Tapii,Comm ctrls.

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
Generalusing windows authenticationmemberjikubhai22:29 27 Sep '07  
Generaldatabase need more...memberpk_mittal21:27 19 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Oct 2006
Editor:
Copyright 2006 by Joshy_geo
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project