|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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
IntroductionIt 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 dllYou 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 { }
|
|||||||||||||||||||||||||||||||||||||||||||||||||||