SQLDMO Create Sql server Database C# code




Sample code to create an sql server Database using SQLDMO Library.
Add 'SQLDMO.dll' refference to your Project



public bool CreateDb(string serverName, string Uid, string Pwd, string dbName)
{
bool result = true;

SQLDMO.SQLServer gSQLServerDMO = new SQLDMO.SQLServer();
SQLDMO.Database nDatabase = new SQLDMO.Database();
SQLDMO.DBFile nDBFileData = new SQLDMO.DBFile();
SQLDMO.LogFile nLogFile = new SQLDMO.LogFile();


try
{
gSQLServerDMO.LoginSecure = true;
gSQLServerDMO.Connect(serverName, Uid, Pwd);

nDatabase.Name = dbName;

nDBFileData.Name = dbName;

nDBFileData.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + dbName + "_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 = dbName + "Log";

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

nLogFile.Size = 2;

nDatabase.TransactionLog.LogFiles.Add(nLogFile);

gSQLServerDMO.Databases.Add(nDatabase);

MessageBox.Show("Database Created Sucessfully);

gSQLServerDMO.DisConnect();

}

catch (Exception SQLDBException)
{

MessageBox.Show(SQLDBException.Message);

result = false;

}

finally
{

}


return (result);
}



Comments

Author: guru prasad30 Jan 2009 Member Level: Gold   Points : 1

This code is very nice to take back...

But i'm unable to add the 'SQLDMO.dll' to my project.

can you tellme the way to install & add ''SQLDMO.dll' to my project to take backup...

Author: Prajith mp10 Feb 2009 Member Level: Silver   Points : 2

you can find the SQLDMO.dll in
C:\Program Files\Microsoft SQL Server\80\Tools\Binn
or download it from
http://www.driverskit.com/dll/sqldmo.dll/3449.html

to add refference to a project
From project menu select "add refference" -> select the browse tab and select the dll file from the location and add to the project





  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: