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
{
}

abhilash svPosted Oct 12, 2008, 7:59 AM
Hello Sir .. i am beginner of c#.Net..now i am doing one windows application..project..for my client...project is Coin management s/w...i want to supplied in my EXE (published version ) to more than 10 people in coin community.....so they want to add datas in related to coins...i am also used sql2000...so database storage output format is MDF file....i want to merge or combined datas in each and every mdf file content..without duplications.....how can i do usng c#....please give most suitable solutions... for Eg:....i insert coin data in my computer using my EXE...if u add same way is this in your computer...i want to combine data together.. data in mdf file1 ------- 1 ..Example Data1 2..Example Data2 3..Example Data3 4.Example Data4 data in mdf file2 ------- 1 ..Example Data1 2..Example Data5 3..Example Data6 4.Example Data4 ............................... after merging main MDF file is data in mdf file1 ------- 1 ..Example Data1 2..Example Data2 3..Example Data3 4.Example Data4 5..Example Data5 6.Example Data6 PLEASE GIVE PROPER WAY OR SOLUTION OR CODE Reply send this email:[email protected]
srikanth kuchiPosted Mar 17, 2008, 6:51 AM
plz help me
ashutosh srivastavaPosted Jan 14, 2008, 6:51 AM
please help me out my EmailId([email protected]) or u can send me on c-sharpcorner.com
kedar jandePosted Nov 3, 2007, 4:46 AM
Hello Sir, i am beaginer in asp.net with limited kwnld. i want to create table in sql at runtime in asp.net pls help me out
Vivekanand KilaparthiPosted Dec 29, 2006, 3:36 AM
Hello Sir I am Vivekanand.Kilaparthi, I had a doubt regarding database object. Can you please tell me how can we create a database object(table, view etc) in sql server using C# language. Thanking you