sqlserver备份还原数据库功能封装分享
using system;
using sqldmo;
namespace edtbackuptool
{
/// <summary>
/// backup 的摘要说明。
/// </summary>
public class sqltools
{
public static bool backup(string backfile){
try
{
sqldmo.backup backup = new backupclass();
sqldmo.sqlserver server = new sqlserverclass();
server.loginsecure = true;
server.connect(clientcommon.commonfunctions.datasource,"sa","");
backup.action = sqldmo.sqldmo_backup_type.sqldmobackup_database;
backup.database = clientcommon.commonfunctions.catalog;
backup.files =backfile;
backup.backupsetname =clientcommon.commonfunctions.catalog;
backup.backupsetdescription = "数据库备份";
backup.initialize = true;
backup.sqlbackup(server);
return true;
}
catch(exception ex){
clientcommon.exceptionrecorder.record(ex);
return false;
}
}
public static bool restoredb(string strfilename)
{
sqldmo.sqlserver svr = new sqldmo.sqlserverclass() ;
try
{
svr.loginsecure=true;
svr.connect(clientcommon.commonfunctions.datasource,"sa","") ;
sqldmo.queryresults qr = svr.enumprocesses(-1) ;
int icolpidnum = -1 ;
int icoldbname = 1 ;
for(int i=1;i<=qr.columns;i++)
{
string strname = qr.get_columnname(i) ;
if (strname.toupper().trim() == "spid")
{
icolpidnum = i ;
}
else if (strname.toupper().trim() == clientcommon.commonfunctions.catalog)
{
icoldbname = i ;
}
if (icolpidnum != -1 && icoldbname != -1)
break ;
}
//杀死使用strdbname数据库的进程
for(int i=1;i<=qr.rows;i++)
{
int lpid = qr.getcolumnlong(i,icolpidnum) ;
string strdbname = qr.getcolumnstring(i,icoldbname) ;
if (strdbname.toupper() == clientcommon.commonfunctions.catalog)
{
svr.killprocess(lpid) ;
}
}
sqldmo.restore res = new sqldmo.restoreclass() ;
res.action = 0 ;
res.files = strfilename ;
res.database = clientcommon.commonfunctions.catalog ;
res.replacedatabase = true ;
res.sqlrestore(svr) ;
return true ;
}
catch
{
return false;
}
finally
{
svr.disconnect() ;
}
}
}
}
推荐阅读
-
用Sql Server 2000的数据库备份来还原Sql Server 2005中的数据库 博客分类: sqlserver@数据库 sql server 2000sql server 2005还原数据库
-
SQLserver还原出现报错:“还原数据库报错无法执行BACKUPLOG,因为当前没有数据库备份”的解决办法
-
挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)
-
C# 编写的SqlServer 数据库自动备份服务,带配置,功能强大
-
SQLServer2008数据库备份还原和数据恢复
-
SQLSERVER使用密码加密备份文件以防止未经授权还原数据库
-
SQLServer 2005 自动备份数据库的方法分享(附图解教程)_MySQL
-
SQLSERVER使用密码加密备份文件以防止未经授权还原数据库
-
Ext sqlserver C# 数据库备份还原代码,给大家参考下
-
SQLserver2008数据库备份和还原问题(还原是必须有完整备份)