private void btnBackUp_Click(object sender, System.EventArgs e)
{
if(rtbBack.Text.Trim() ==string.Empty )
{
MessageBox.Show("必须输入备注","提示",MessageBoxButtons.OK ,MessageBoxIcon.Asterisk );
}
else
{
this.Cursor = Cursors.WaitCursor;
this.lblTitle.Text = " 正在进行档案库的数据备份,这可能需要几秒到几十的时间,请稍候...";
this.lblTitle.Visible = true;
this.lblTitle.Refresh();
this.pBar1.Visible = true;
//------------------------------------------------------------------------------------
string strCurrentyTime = DateTime.Now.ToString() ;
string strFileName = "LibraryBookDB" + strCurrentyTime.ToString().Replace("-","").Replace(":","").Replace(" ","")+".Bak";
string strFilePath= Application.StartupPath + ConfigurationSettings.AppSettings["strFilePath"] + strFileName;
string deviceName = "LibraryBookDB.Bak";
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", "");
oSQLServer.Connect(ConfigurationSettings.AppSettings["strServerName"] , "sa", "");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = ConfigurationSettings.AppSettings["strDBName"] ;//数据库名
oBackup.Files = strFilePath;//文件路径
oBackup.BackupSetName = deviceName;//备份名称
oBackup.BackupSetDescription = remark;//备份描述
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch(System.Exception ex)
{
//Common.ShowMsg("数据备份失败: " + ex.ToString());
MessageBox.Show ("数据备份失败: " + ex.ToString()) ;
return ;
}
finally
{
oSQLServer.DisConnect();
}
//-----------------------------------------------------------------------------------
//写入xml文件
DBBackUpAndRestoreClass myDBBackUpAndRestoreClass = new DBBackUpAndRestoreClass();
myDBBackUpAndRestoreClass.InsertXmlFile(strFileName ,ConfigurationSettings.AppSettings["strFilePath"],strCurrentyTime,rtbBack.Text );
//在datagrid控件中显示出来
frmDBBackUpAndRestore_Load(sender,e);
//------------------------------------------------------------------------------------
this.lblTitle.Visible = false;
this.pBar1.Visible = false;
this.Cursor = Cursors.Default;
this.rtbBack.Text = string.Empty;
this.rtbBack.Focus();
}
}
/// <summary>
/// 显示备份进度条
/// </summary>
private void Step(string message,int percent)
{
this.pBar1.Value = percent ;
}
private void btnRestore_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("你确定要恢复数据库吗?","提示",MessageBoxButtons.YesNo ,MessageBoxIcon.Question ) == DialogResult.Yes )
{
if(dgrDataShow.IsSelected(dgrDataShow.CurrentRowIndex))
{
DataRow myDataRow = ((DataTable)(dgrDataShow.DataSource)).Rows[dgrDataShow.CurrentRowIndex] ;
this.Cursor = Cursors.WaitCursor;
this.lblTitle.Text = " 正在进行档案库的数据还原,这可能需要几秒到几十的时间,请稍候...";
this.lblTitle.Visible = true;
this.lblTitle.Refresh();
this.pBar1.Visible = true;
//------------------------------------------------------------------------------------
string strFileName = myDataRow["FileName"].ToString() ;
//string strFilePath = Application.StartupPath + myDataRow["文件路径"].ToString() + strFileName ;
string strFilePath = myDataRow["FilePath"].ToString();
string strTureFilePath = Application.StartupPath + strFilePath + strFileName;
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(ConfigurationSettings.AppSettings["strServerName"], "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() == ConfigurationSettings.AppSettings["strDBName"].ToString() .ToUpper())
oSQLServer.KillProcess(lPID) ;
}
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = ConfigurationSettings.AppSettings["strDBName"].ToString() ;//数据库名
oRestore.Files = strTureFilePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
MessageBox.Show("数据库恢复成功,软件将重新启动。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Asterisk);
//退出程序
Application.Exit();
//重新启动程序
Process.Start("LibraryBookManageSystem.exe");
}
catch(System.Exception ex)
{
//Common.ShowMsg("数据还原失败: " + ex.ToString());
MessageBox.Show("数据还原失败: " + ex.ToString());
}
finally
{
oSQLServer.DisConnect();
}
//------------------------------------------------------------------------------------
this.lblTitle.Visible = false;
this.pBar1.Visible = false;
this.Cursor = Cursors.Default;
}
else
{
MessageBox.Show("请选择要恢复的数据库备份","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk );
}
}
}