SQL2000数据库备份和还原

时间:2021-08-25 20:48:38
  1  using System;
  2  using System.Data;
  3  using System.Configuration;
  4  using System.Collections;
  5  using System.Web;
  6  using System.Web.Security;
  7  using System.Web.UI;
  8  using System.Web.UI.WebControls;
  9  using System.Web.UI.WebControls.WebParts;
 10  using System.Web.UI.HtmlControls;
 11 
 12  public partial class databasemanager : System.Web.UI.Page
 13  {
 14      protected void Page_Load(object sender, EventArgs e)
 15      {
 16 
 17      }
 18 
 19      ///  < summary >
 20      /// 数据库恢复和备份
 21      ///  </ summary >
 22     
 23          #region SQL数据库备份
 24          ///  < summary >
 25          /// SQL数据库备份
 26          ///  </ summary >
 27          ///  < param  name ="ServerIP" > SQL服务器IP或(Localhost) </ param >
 28          ///  < param  name ="LoginName" > 数据库登录名 </ param >
 29          ///  < param  name ="LoginPass" > 数据库登录密码 </ param >
 30          ///  < param  name ="DBName" > 数据库名 </ param >
 31          ///  < param  name ="BackPath" > 备份到的路径 </ param >
 32          public static void SQLBACK(string ServerIP, string LoginName, string LoginPass, string DBName, string BackPath)
 33          {
 34              SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
 35              SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
 36              try
 37              {
 38                  oSQLServer.LoginSecure = false;
 39                  oSQLServer.Connect(ServerIP, LoginName, LoginPass);
 40                  oBackup.Database = DBName;
 41                  oBackup.Files = BackPath;
 42                  oBackup.BackupSetName = DBName;
 43                  oBackup.BackupSetDescription = "数据库备份";
 44                  oBackup.Initialize = true;
 45                  oBackup.SQLBackup(oSQLServer);
 46              }
 47              catch (Exception e)
 48              {
 49                  throw new Exception(e.ToString());
 50              }
 51              finally
 52              {
 53                  oSQLServer.DisConnect();
 54              }
 55          }
 56          #endregion
 57          #region SQL恢复数据库
 58          ///  < summary >
 59          /// SQL恢复数据库
 60          ///  </ summary >
 61          ///  < param  name ="ServerIP" > SQL服务器IP或(Localhost) </ param >
 62          ///  < param  name ="LoginName" > 数据库登录名 </ param >
 63          ///  < param  name ="LoginPass" > 数据库登录密码 </ param >
 64          ///  < param  name ="DBName" > 要还原的数据库名 </ param >
 65          ///  < param  name ="BackPath" > 数据库备份的路径 </ param >
 66          public static void SQLDbRestore(string ServerIP, string LoginName, string LoginPass, string DBName, string BackPath)
 67          {
 68 
 69              SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
 70              SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
 71              try
 72              {
 73                  oSQLServer.LoginSecure = false;
 74                  oSQLServer.Connect(ServerIP,LoginName,LoginPass);
 75                  orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
 76                  orestore.Database = DBName;
 77                  orestore.Files = BackPath;
 78                  orestore.FileNumber = 1;
 79                  orestore.ReplaceDatabase = true;
 80                  orestore.SQLRestore(oSQLServer);
 81              }
 82              catch (Exception e)
 83              {
 84                  throw new Exception(e.ToString());
 85              }
 86              finally
 87              {
 88                  oSQLServer.DisConnect();
 89              }
 90          }
 91 
 92          #endregion
 93 
 94      protected void Button_back_Click(object sender, EventArgs e)
 95      {
 96          try
 97          {
 98              SQLBACK(serverIP.Text, servername.Text, serverpwd.Text, databasename.Text, databasepath.Text);
 99              Response.Write("数据库备份成功!");
100          }
101          catch (Exception exp)
102          {
103              Response.Write("数据库备份失败!"+exp.Message);
104          }
105      }
106      protected void Button_go_Click(object sender, EventArgs e)
107      {
108          try
109          {
110              SQLDbRestore(serverIP.Text, servername.Text, serverpwd.Text, databasename.Text, databasepath.Text);
111              Response.Write("数据库还原成功!");
112          }
113          catch (Exception exp)
114          {
115              
116              Response.Write("数据库还原失败!"+exp.Message);;
117          }
118      }
119  }
120