数据库:sql server ;引用sqldmo
◆数据备份 :
/// <summary>
/// 备份数据库
/// </summary>
private void btnbackup_click(object sender, system.eventargs e)
{
this.cursor = cursors.waitcursor;
this.label1.text = " 正在进行档案库的数据备份,这可能需要几秒到几十的时间,请稍候...";
this.label1.visible = true;
this.label1.refresh();
this.pbar1.visible = true;
//------------------------------------------------------------------------------------
string selfname = “d:/northwindbak“;
string devicename = “northwindbak“;
string remark = "备份测试";
//◆数据备份:
sqldmo.backup obackup = new sqldmo.backupclass();
sqldmo.sqlserver osqlserver = new sqldmo.sqlserverclass();
obackup.action = 0 ;
obackup.initialize = true ;
sqldmo.backupsink_percentcompleteeventhandler pceh = new sqldmo.backupsink_percentcompleteeventhandler(step);
obackup.percentcomplete += pceh;
try
{
osqlserver.loginsecure = false;
osqlserver.connect(common.mysettings.sqlservername, "sa", "");
obackup.action = sqldmo.sqldmo_backup_type.sqldmobackup_database;
obackup.database = "northwind";//数据库名
obackup.files = selfname;//文件路径
obackup.backupsetname = devicename;//备份名称
obackup.backupsetdescription = remark;//备份描述
obackup.initialize = true;
obackup.sqlbackup(osqlserver);
}
catch(system.exception ex)
{
common.showmsg("数据备份失败:/n" + ex.tostring());
}
finally
{
osqlserver.disconnect();
}
//------------------------------------------------------------------------------------
this.label1.visible = false;
this.pbar1.visible = false;
this.cursor = cursors.default;
}
/// <summary>
/// 显示备份进度条
/// </summary>
private void step(string message,int percent)
{
this.pbar1.value = percent ;
}
◆数据还原 :
/// <summary>
/// 还原数据库
/// </summary>
private void btnrestore_click(object sender, system.eventargs e)
{
this.cursor = cursors.waitcursor;
this.label1.text = " 正在进行档案库的数据还原,这可能需要几秒到几十的时间,请稍候...";
this.label1.visible = true;
this.label1.refresh();
this.pbar1.visible = true;
//------------------------------------------------------------------------------------
string filename = "northwindbak";
string filepath = "d:/northwindbak";
string remark = "备份测试";
sqldmo.restore orestore = new sqldmo.restoreclass();
sqldmo.sqlserver osqlserver = new sqldmo.sqlserverclass();
orestore.action = 0 ;
sqldmo.restoresink_percentcompleteeventhandler pceh = new sqldmo.restoresink_percentcompleteeventhandler(step);
orestore.percentcomplete += pceh;
try
{
osqlserver.connect(common.mysettings.sqlservername, "sa", "");
sqldmo.queryresults qr = osqlserver.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() == "dbname")
{
icoldbname = i ;
}
if (icolpidnum != -1 && icoldbname != -1)
break ;
}
for(int i=1;i<=qr.rows;i++)
{
int lpid = qr.getcolumnlong(i,icolpidnum) ;
string strdbname = qr.getcolumnstring(i,icoldbname) ;
if (strdbname.toupper() == "northwind".toupper())
osqlserver.killprocess(lpid) ;
}
orestore.action = sqldmo.sqldmo_restore_type.sqldmorestore_database;
orestore.database = "northwind";
orestore.files = filepath;
orestore.filenumber = 1;
orestore.replacedatabase = true;
orestore.sqlrestore(osqlserver);
}
catch(system.exception ex)
{
common.showmsg("数据还原失败:/n" + ex.tostring());
}
finally
{
osqlserver.disconnect();
}
//------------------------------------------------------------------------------------
this.label1.visible = false;
this.pbar1.visible = false;
this.cursor = cursors.default;
}
/// <summary>
/// 显示还原进度条
/// </summary>
private void step(string message,int percent)
{
this.pbar1.value = percent ;
}
新闻热点
疑难解答
图片精选