.Net下的数据备份和还原
2008-05-01 04:33
495 查看
.Rca835{display:none;}
◆数据备份:
///<summary>
///备份数据库
///</summary>
privatevoidbtnBackUp_Click(objectsender,System.EventArgse)
{
this.Cursor=Cursors.WaitCursor;
this.label1.Text=" 正在进行档案库的数据备份,这可能需要几秒到几十的时间,请稍候...";
this.label1.Visible=true;
this.label1.Refresh();
this.pBar1.Visible=true;
//------------------------------------------------------------------------------------
stringselfName=“D:/NorthwindBak“;
stringdeviceName=“NorthwindBak“;
stringremark="备份测试";
//◆数据备份:
SQLDMO.BackupoBackup=newSQLDMO.BackupClass();
SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
oBackup.Action=0;
oBackup.Initialize=true;
SQLDMO.BackupSink_PercentCompleteEventHandlerpceh=newSQLDMO.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.Exceptionex)
{
Common.ShowMsg("数据备份失败:/n"+ex.ToString());
}
finally
{
oSQLServer.DisConnect();
} //------------------------------------------------------------------------------------
this.label1.Visible=false;
this.pBar1.Visible=false;
this.Cursor=Cursors.Default;
}
///<summary>
///显示备份进度条
///</summary>
privatevoidStep(stringmessage,intpercent)
{
this.pBar1.Value=percent;
}
◆数据还原:
///<summary>
///还原数据库
///</summary>
privatevoidbtnRestore_Click(objectsender,System.EventArgse)
{
this.Cursor=Cursors.WaitCursor;
this.label1.Text=" 正在进行档案库的数据还原,这可能需要几秒到几十的时间,请稍候...";
this.label1.Visible=true;
this.label1.Refresh();
this.pBar1.Visible=true;
//------------------------------------------------------------------------------------
stringfileName="NorthwindBak";
stringfilePath="D:/NorthwindBak";
stringremark="备份测试";
SQLDMO.RestoreoRestore=newSQLDMO.RestoreClass();
SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
oRestore.Action=0;
SQLDMO.RestoreSink_PercentCompleteEventHandlerpceh=newSQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
oRestore.PercentComplete+=pceh;
try
{
oSQLServer.Connect(Common.MySettings.SqlServerName,"sa","");
SQLDMO.QueryResultsqr=oSQLServer.EnumProcesses(-1);
intiColPIDNum=-1;
intiColDbName=-1;
//杀死其它的连接进程
for(inti=1;i<=qr.Columns;i++)
{
stringstrName=qr.get_ColumnName(i);
if(strName.ToUpper().Trim()=="SPID")
{
iColPIDNum=i;
}
elseif(strName.ToUpper().Trim()=="DBNAME")
{
iColDbName=i;
}
if(iColPIDNum!=-1&&iColDbName!=-1)
break;
} for(inti=1;i<=qr.Rows;i++)
{
intlPID=qr.GetColumnLong(i,iColPIDNum);
stringstrDBName=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.Exceptionex)
{
Common.ShowMsg("数据还原失败:/n"+ex.ToString());
}
finally
{
oSQLServer.DisConnect();
}
//------------------------------------------------------------------------------------
this.label1.Visible=false;
this.pBar1.Visible=false;
this.Cursor=Cursors.Default;
}
///<summary>
///显示还原进度条
///</summary>
privatevoidStep(stringmessage,intpercent)
{
this.pBar1.Value=percent;
}
◆数据备份:
///<summary>
///备份数据库
///</summary>
privatevoidbtnBackUp_Click(objectsender,System.EventArgse)
{
this.Cursor=Cursors.WaitCursor;
this.label1.Text=" 正在进行档案库的数据备份,这可能需要几秒到几十的时间,请稍候...";
this.label1.Visible=true;
this.label1.Refresh();
this.pBar1.Visible=true;
//------------------------------------------------------------------------------------
stringselfName=“D:/NorthwindBak“;
stringdeviceName=“NorthwindBak“;
stringremark="备份测试";
//◆数据备份:
SQLDMO.BackupoBackup=newSQLDMO.BackupClass();
SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
oBackup.Action=0;
oBackup.Initialize=true;
SQLDMO.BackupSink_PercentCompleteEventHandlerpceh=newSQLDMO.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.Exceptionex)
{
Common.ShowMsg("数据备份失败:/n"+ex.ToString());
}
finally
{
oSQLServer.DisConnect();
} //------------------------------------------------------------------------------------
this.label1.Visible=false;
this.pBar1.Visible=false;
this.Cursor=Cursors.Default;
}
///<summary>
///显示备份进度条
///</summary>
privatevoidStep(stringmessage,intpercent)
{
this.pBar1.Value=percent;
}
◆数据还原:
///<summary>
///还原数据库
///</summary>
privatevoidbtnRestore_Click(objectsender,System.EventArgse)
{
this.Cursor=Cursors.WaitCursor;
this.label1.Text=" 正在进行档案库的数据还原,这可能需要几秒到几十的时间,请稍候...";
this.label1.Visible=true;
this.label1.Refresh();
this.pBar1.Visible=true;
//------------------------------------------------------------------------------------
stringfileName="NorthwindBak";
stringfilePath="D:/NorthwindBak";
stringremark="备份测试";
SQLDMO.RestoreoRestore=newSQLDMO.RestoreClass();
SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
oRestore.Action=0;
SQLDMO.RestoreSink_PercentCompleteEventHandlerpceh=newSQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
oRestore.PercentComplete+=pceh;
try
{
oSQLServer.Connect(Common.MySettings.SqlServerName,"sa","");
SQLDMO.QueryResultsqr=oSQLServer.EnumProcesses(-1);
intiColPIDNum=-1;
intiColDbName=-1;
//杀死其它的连接进程
for(inti=1;i<=qr.Columns;i++)
{
stringstrName=qr.get_ColumnName(i);
if(strName.ToUpper().Trim()=="SPID")
{
iColPIDNum=i;
}
elseif(strName.ToUpper().Trim()=="DBNAME")
{
iColDbName=i;
}
if(iColPIDNum!=-1&&iColDbName!=-1)
break;
} for(inti=1;i<=qr.Rows;i++)
{
intlPID=qr.GetColumnLong(i,iColPIDNum);
stringstrDBName=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.Exceptionex)
{
Common.ShowMsg("数据还原失败:/n"+ex.ToString());
}
finally
{
oSQLServer.DisConnect();
}
//------------------------------------------------------------------------------------
this.label1.Visible=false;
this.pBar1.Visible=false;
this.Cursor=Cursors.Default;
}
///<summary>
///显示还原进度条
///</summary>
privatevoidStep(stringmessage,intpercent)
{
this.pBar1.Value=percent;
}
相关文章推荐
- .Net下的数据备份和还原
- .Net下的数据备份和还原
- .Net下的数据备份和还原
- .Net下的数据备份和还原
- mysql 数据备份与还原
- RMAN备份与恢复(5)——将数据文件或表空间还原到新位置!
- Linux系统下通过命令行对mysql数据进行备份和还原
- 史上最简单的 MySQL 教程(三十五)「数据备份与还原(上)」
- 06、MySQL视图及数据备份还原
- Sybase数据库的备份和还原
- 将备份数据还原到数据库的步骤
- IIS7:备份还原,导入导出应用池数据,导出导入网站配置数据
- mysql数据备份与还原
- Xtrabackup数据库的数据备份和还原
- Xtrabackup数据库的数据备份和还原
- .Net下的数据备份和还原
- MySQL基础之第16章 数据备份与还原
- SQL技能天天练(2)---数据备份与还原
- mysql数据备份与还原
- 【MySQL基础】数据的备份与还原