当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > .Net下的数据备份和还原

ASP.NET
.Net中使用com组件后发生System.ArithmeticException异常的解决办法
SQL Server.net 和 OLE DB.net连接数据库的比较
后台更新DataTable行内容的方法
敏捷软件开发(原则,模式与实践)笔记1
确保文本框输入值为数值的代码
XML和数据库之间相互的映射
让你的.NET程序兼容不同版本的Dll文件。
.NET 的数据访问应用程序块(Data Access Application Block)
用控件仅一条指令实现界面换肤和多语言版本(YFSkins)
Microsoft User Interface Process Application Block 研究(3)
分享:处理Excel方法小结
基于ASP.NET实现全球化
.net 里面 protected private 的变量也可以访问(新发现)。
关于C#中{0}和{1}的问题初次在此发贴,问题对你易对我难,求救了
使用C#代码实现增加用户帐号
全世界都在关注-微软重大产品发布
教你做Rational Rose(UML Design)
OLE DB取得数据库的架构信息
VB 从零开始编外挂(三)
XPath序列之四

ASP.NET 中的 .Net下的数据备份和还原


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 122 ::
收藏到网摘: n/a

  数据库: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 ;
  }