在网上传闻SQLDMO是个好东西,当时没有注意这个传闻是什么时候了,后来才在微软的官网上看见,从SQL Server2008开始就不用SQLDMO了,取而代之的是SMO。无奈了,还写了个Helper。感觉DMO用起来比用SQLCMD少输些命令,而且如果用ADO.NET调用存储过程的话,好像还比DMO方便。

这个SQLDMO绝不是像别的dll那样直接添加引用的,它是个COM组件,要注册了才能使用,这说明了,凡是要跑带这个组件的程序,都要先注册一下组件。

在网上下一个SQLDOM的压缩包,解压后

第一步:首先将msvcr71.dll, SQLDMO.DLL, Resources\2052\sqldmo.rll,Resources\1033\sqldmo.rll 拷贝到C:\Program Files\Microsoft SQL Server\80\Tools\Binn目录。
下载SQLDMO文件
第二步:打开开始,在运行中输入 regsvr32 "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqldmo.dll" 注册sqldmo.dll。
SQLDMOHelper-LMLPHP

好了,是时候粘贴代码了

     class SQLDMOHelper
{ //增加
public static void CreateDB(string dbName, string hostName, string instanceName, string username, string password)
{
SQLServer server = null;
Application app=null;
try
{
server = new SQLServer();
app = new ApplicationClass();
ConfigServer(server, hostName, instanceName, username, password);
string dbPath = server.Registry.SQLDataRoot + @"\DATA\" + dbName; foreach (Database db in server.Databases)
if (db.Name == dbName) return; Database database = new DatabaseClass();
DBFile file = new DBFileClass();
LogFile log = new LogFileClass(); database.Name = dbName;
file.Name = dbName + "file";
file.PhysicalName = dbPath + "_Data.mdf";
file.PrimaryFile = true;
file.FileGrowthType = ;
file.FileGrowth = ;
database.FileGroups.Item("primary").DBFiles.Add(file);
log.Name = dbName + "log";
log.PhysicalName = dbPath + "_Log.ldf";
database.TransactionLog.LogFiles.Add(log);
server.Databases.Add(database); }
catch
{
throw;
}
finally
{
if (server != null)
{
server.DisConnect();
server.Close();
}
if (app != null)
app.Quit();
} } public static void CreateDB(string dbName, string hostName, string instanceName)
{
CreateDB(dbName, hostName, instanceName, "", "");
} //删除 public static void DeleteDB(string dbName, string hostName, string instanceName, string username, string password)
{
SQLServer server=null;
try
{
server = new SQLServerClass();
ConfigServer(server, hostName, instanceName, username, password);
Database database = server.Databases.Item(dbName, null) as Database; QueryResults queryRestlts = server.EnumProcesses(-);
int iColPIDNum = -;
int iColDbName = -;
for (int i = ; i <= queryRestlts.Columns; i++)
{
string strName = queryRestlts.get_ColumnName(i);
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i;
}
else if (strName.ToUpper().Trim() == "DBNAME")
{
iColDbName = i;
}
if (iColPIDNum != - && iColDbName != -)
break;
} for (int i = ; i <= queryRestlts.Rows; i++)
{
int lPID = queryRestlts.GetColumnLong(i, iColPIDNum);
string strDBName = queryRestlts.GetColumnString(i, iColDbName);
if (strDBName.ToUpper() == dbName)
server.KillProcess(lPID);
} database.Remove();
}
catch
{
throw;
}
finally
{
if (server != null)
{
server.DisConnect();
server.Close();
}
} } public static void DeleteDB(string dbName, string hostName, string instanceName)
{
DeleteDB(dbName, hostName, instanceName, "", "");
} //附加
public static void AttachDB(string dbName,string dbFileName,string hostName,string instanceName,string username,string password)
{
SQLServer server = null;
try
{
server = new SQLServer();
ConfigServer(server, hostName, instanceName, username, password);
server.AttachDBWithSingleFile(dbName, dbFileName);
}
catch
{
throw;
}
finally
{
if (server != null)
{
server.DisConnect();
server.Close();
}
}
} public static void AttachDB(string dbName, string dbFileName, string hostName, string instanceName)
{
AttachDB(dbName, dbFileName, hostName, instanceName, "", "");
} //分离
public static void DetachDB(string dbName, string dbFileName, string hostName, string instanceName, string username, string password)
{
SQLServer server = null;
try
{
server = new SQLServer();
ConfigServer(server, hostName, instanceName, username, password);
server.DetachDB(dbName);
}
catch
{
throw;
}
finally
{
if (server != null)
{
server.DisConnect();
server.Close();
}
}
} public static void DetachDB(string dbName, string dbFileName, string hostName, string instanceName)
{
DetachDB(dbName, dbFileName, hostName, instanceName, "", "");
} //脱机
//联机 /// <summary>
/// 数据库全备份 失败则会抛异常
/// </summary>
public static void BackupDB(string fileName, string dbName, string hostName, string instanceName, string username, string password)
{
SQLServer server=null;
Backup backup=null;
try
{
server = new SQLServer();
backup = new Backup(); ConfigServer(server, hostName, instanceName, username, password);
backup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
backup.Database = dbName;
backup.BackupSetName = dbName;
backup.BackupSetDescription = "数据库备份";
backup.Files = fileName;
backup.Initialize = true;
backup.SQLBackup(server);
}
catch
{
throw;
}
finally
{
if (server != null)
{
server.DisConnect();
server.Close();
}
}
} public static void BackupDB(string fileName, string dbName, string hostName, string instanceName)
{
BackupDB(fileName, dbName, hostName, instanceName,"","");
} /// <summary>
/// 还原数据库 失败会抛异常
/// </summary>
public static void RestoreDB(string fileName, string dbName, string dbFileName, string hostName, string instanceName, string username, string password)
{
SQLServer server = null;
Restore restore = null;
try
{
server = new SQLServerClass();
restore = new RestoreClass();
ConfigServer(server, hostName, instanceName, username, password); QueryResults queryRestlts = server.EnumProcesses(-);
int iColPIDNum = -;
int iColDbName = -;
for (int i = ; i <= queryRestlts.Columns; i++)
{
string strName = queryRestlts.get_ColumnName(i);
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i;
}
else if (strName.ToUpper().Trim() == "DBNAME")
{
iColDbName = i;
}
if (iColPIDNum != - && iColDbName != -)
break;
} for (int i = ; i <= queryRestlts.Rows; i++)
{
int lPID = queryRestlts.GetColumnLong(i, iColPIDNum);
string strDBName = queryRestlts.GetColumnString(i, iColDbName);
if (strDBName.ToUpper() == dbName)
server.KillProcess(lPID);
} restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
restore.Database = dbName;
restore.Files = fileName;
restore.FileNumber = ;
restore.ReplaceDatabase = true;
restore.SQLRestore(server);
}
catch
{
throw;
}
finally
{
if (server != null)
{
server.DisConnect();
server.Close();
}
}
} public static void RestoreDB(string fileName, string dbName, string dbFileName, string hostName, string instanceName)
{
RestoreDB(fileName, dbName, dbFileName, hostName,instanceName,"","");
} private static void ConfigServer(SQLServer server, string hostName, string instanceName, string userName = "", string password = "")
{
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password))
server.LoginSecure = true;
else
{
server.Login = userName;
server.Password = password;
}
server.Connect(hostName + "\\" + instanceName);
}
}
04-25 14:04