VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#中结合使用SQLDMO实现备份、还原SQL Server数据库

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceMagicbit.Framework
{
  publicpartialclassDBTools:Form
  {
    privatestaticDBTools_Instance=null;   
    publicstaticDBToolsInstance()   
    {
      if(_Instance==null)
      {
        _Instance=newDBTools();
      }
      else
      {
        MessageBox.Show("已经有一个实例在运行!");
      }
      return_Instance;
    }
    
    publicDBTools()
    {
      InitializeComponent();
    }
    privatevoidBackAndRecoverDB_Load(objectsender,EventArgse)
    {
      this.txtSavePath.Text=Application.StartupPath;
      //this.GetSQLServerList();
    }
    privatevoidGetSQLServerList()
    {
      //getallavailableSQLServers  
      SQLDMO._ApplicationsqlApp=newSQLDMO.ApplicationClass();
      SQLDMO.NameListsqlServers=sqlApp.ListAvailableSQLServers();
      for(inti=0;i<sqlServers.Count;i++)
      {
        objectsrv=sqlServers.Item(i+1);
        if(srv!=null)
        {
          this.cboServers.Items.Add(srv);
        }
      }
      if(this.cboServers.Items.Count>0)
        this.cboServers.SelectedIndex=0;
      else
        this.cboServers.Text="<NoavailableSQLServers>";
    
    }
    privatevoidGetBackUpDB()
    {
      SQLDMO.ApplicationsqlApp =newSQLDMO.ApplicationClass();
      SQLDMO.SQLServersrv    =newSQLDMO.SQLServer();
      srv.Connect(this.cboServers.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassword.Text.Trim());
      foreach(SQLDMO.Databasedbinsrv.Databases)
      {
        if(db.Name!=null)
          this.cboDatabase.Items.Add(db.Name);
      }
    
    }
    privatevoidpictureBox1_Click(objectsender,EventArgse)
    {
      MessageBox.Show("欢迎使用数据库备份、还原工具,本工具将协助你备份和还原数据库,确保数据安全!","备份您的数据库");
    }
    privatevoidbutton1_Click(objectsender,EventArgse)
    {
      this.GetBackUpDB();
    }
    privatevoidBackUpDB()
    {
      stringselfName=this.txtSavePath.Text.Trim()+@""+this.cboDatabase.Text.Trim()+"_"+System.DateTime.Now.ToString("yyyyMMddHHmmss")+".DAT";
      stringdeviceName=this.cboDatabase.Text.Trim()+"bak";
      stringremark="数据备份";
      //BACKUPDB
      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(this.cboServers.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassword.Text.Trim());
        oBackup.Action=SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
        oBackup.Database=this.cboDatabase.Text.Trim();//数据库名
        oBackup.Files=selfName;//文件路径
        oBackup.BackupSetName=deviceName;//备份名称
        oBackup.BackupSetDescription=remark;//备份描述
        oBackup.Initialize=true;
        oBackup.SQLBackup(oSQLServer);
      }
      catch(System.Exceptionex)
      {
        MessageBox.Show("数据备份失败: "+ex.ToString());
      }
      finally
      {
        oSQLServer.DisConnect();
      }
    }
    privatevoidStep(stringmessage,intpercent)
    {
      this.progressBar1.Value=percent;
    }
    privatevoidbutton2_Click(objectsender,EventArgse)
    {
      this.Cursor=Cursors.WaitCursor;
      this.label6.Visible=true;
      this.progressBar1.Visible=true;
      this.BackUpDB();
      this.Cursor=Cursors.Default;
      this.label6.Text="备份已完成.";
    }
    publicvoidRestoreDB()
    {
      stringfilePath=this.txtBackUpFile.Text.Trim();
      SQLDMO.RestoreoRestore=newSQLDMO.RestoreClass();
      SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
      oRestore.Action=0;
      SQLDMO.RestoreSink_PercentCompleteEventHandlerpceh=newSQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
      oRestore.PercentComplete+=pceh;
      try
      {
      oSQLServer.Connect(this.cboServers.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassword.Text.Trim());
      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()=="CgRecord".ToUpper())
          oSQLServer.KillProcess(lPID);
        }
        oRestore.Action=SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
        oRestore.Database=this.cboDBtoBackup.Text;
        oRestore.Files=filePath;
        oRestore.FileNumber=1;
        oRestore.ReplaceDatabase=true;
        oRestore.SQLRestore(oSQLServer);
      }
      catch(System.Exceptionex)
      {
        MessageBox.Show("数据还原失败: "+ex.ToString());
      }
      finally
      {
        oSQLServer.DisConnect();
      }
  
    }
    privatevoidbutton3_Click(objectsender,EventArgse)
    {
      this.folderBrowserDialog1.Description="请选择备份文件存放目录";
      this.folderBrowserDialog1.ShowNewFolderButton=true;
      this.folderBrowserDialog1.ShowDialog();
      this.txtSavePath.Text=this.folderBrowserDialog1.SelectedPath;
    }
    privatevoidbutton4_Click(objectsender,EventArgse)
    {
      this.openFileDialog1.DefaultExt="*.dat";
      this.openFileDialog1.Title="请选择要还原的数据库备份文件.";
      this.openFileDialog1.ShowDialog();
      this.txtBackUpFile.Text=this.openFileDialog1.FileName;
    }
    privatevoidbutton5_Click(objectsender,EventArgse)
    {
      this.Cursor=Cursors.WaitCursor;
      this.label6.Visible=true;
      this.progressBar1.Visible=true;
      this.RestoreDB();
      this.Cursor=Cursors.Default;
      this.label6.Text="还原已完成.";
    }
  }
}



相关教程