在网上传闻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。

好了,是时候粘贴代码了

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

SQLDMOHelper的更多相关文章

随机推荐

  1. Android Multiple Screens Android 屏幕适配的一些总结

    作为一名Android应用开发程序猿,最痛苦的事莫过于在屏幕适配了,这与历史原因有关,具体就不深究了. 直到最近才搞明白dpi是怎么换算的,在开发的过程中,一个应用运行的屏幕标准应该是分辨率为320x ...

  2. js实现hash

    由于项目中用到了hash,自己实现了一个. Hash = function () { } Hash.prototype = { constructor: Hash, add: function (k, ...

  3. Mac OSX系统下SVN客户端SCPlugin问题

    装上SCPlugin后,Checkout老是出现这种错误: Server certificate verification failed: certificate issued for a diffe ...

  4. ubuntu 12 JDK 编译

    下载openjdk源码 http://jdk7.java.net/source.html 安装Ubuntu上面的依赖包: .参考原书 环境变量配置: .去www.hzbook.com上面将深入理解ja ...

  5. 【原】Jqxgrid在Java服务器端分页

    研究这个后台分页一天多,特此写个文章记录备忘 jsp页面中有两个需要注意的地方:一个是source中beforeprocessing,另一个是rendergridrows中数据的获取. 说明:grid ...

  6. 【NLP】word2vec

    http://blog.csdn.net/mytestmy/article/details/26969149?utm_source=tuicool&utm_medium=referral

  7. 基于LoadRunner构建接口测试框架

    基于LoadRunner构建接口测试框架 http://www.docin.com/p-775544153.html

  8. Python Flask UnicodeDecodeError 编码错误解决

    折腾Python做快速Web开发.最后定下来用Flask,相对教程全面. utf8编码上遇到问题,所有文件已经是utf8编码保存,加载css.js等静态文件,如果用GBK编码就正常:用utf8就报Un ...

  9. Solution to “VirtualBox can't operate in VMX root mode” error in Windows 7

    I was trying out various virtualization solutions on Windows 7, including Microsoft Virtual PC and V ...

  10. sencha touch api 使用指南

    本文主要讲解如何使用sencha touch的api以及如何查看api中官方示例源码 前期准备 1.sdk 下载地址:http://www.sencha.com/products/touch/down ...