qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          用Asp.net還原與恢復sqlserver數據庫

          上次做了個項目,涉及到數據庫的還原和恢復,到網上找了一下,是利用SQLDMO實現的,只要添加SQLDMO引用就好了,然后利用下邊的類的方法就可以實現了。
            我把原作者的類擴充了一下,可以自動識別web.config里 的數據庫連接字符串,可以通過變量設置還原恢復的信息。
            需要注意的時還原,還原的時候問題最大了,有別的用戶使用數據庫的時候無法還原,解決辦法就是在MASTER數據庫中添加一個存儲過程:
          createprockillspid(@dbnamevarchar(20))
          as
          begin
          declare@sqlnvarchar(500)
          declare@spidint
          set@sql='declaregetspidcursorfor
          selectspidfromsysprocesseswheredbid=db_id('''+@dbname+''')'
          exec(@sql)
          opengetspid
          fetchnextfromgetspidinto@spid
          while@@fetch_status<>-1
          begin
          exec('kill'+@spid)
          fetchnextfromgetspidinto@spid
          end
          closegetspid
          deallocategetspid
          end
          GO
            在還原之前先執行這個存儲過程,需要傳遞dbname,就是你的數據庫的名字。下邊是類的原代碼:(web.config里的數據庫連接字符串是constr)
          using System;
          using System.Configuration;
          using System.Data.SqlClient;
          using System.Data;
          namespace web.base_class
          {
          /// <summary>
          /// DbOper類,主要應用SQLDMO實現對Microsoft SQL Server數據庫的備份和恢復
          /// </summary>
          public class DbOper
          {
          private string server;
          private string uid;
          private string pwd;
          private string database;
          private string conn;
          /// <summary>
          /// DbOper類的構造函數
          /// </summary>
          public DbOper()
          {
          conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
          server=cut(conn,"server=",";");
          uid=cut(conn,"uid=",";");
          pwd=cut(conn,"pwd=",";");
          database=cut(conn,"database=",";");
          }
          public string cut(string str,string bg,string ed)
          {
          string sub;
          sub=str.Substring(str.IndexOf(bg)+bg.Length);
          sub=sub.Substring(0,sub.IndexOf(";"));
          return sub;
          }
          /// <summary>
          /// 數據庫備份
          /// </summary>
          public  bool DbBackup(string url)
          {
          SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
          SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
          try
          {
          oSQLServer.LoginSecure = false;
          oSQLServer.Connect(server,uid, pwd);
          oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
          oBackup.Database = database;
          oBackup.Files = url;//"d:/Northwind.bak";
          oBackup.BackupSetName = database;
          oBackup.BackupSetDescription = "數據庫備份";
          oBackup.Initialize = true;
          oBackup.SQLBackup(oSQLServer);
          return true;
          }
          catch
          {
          return false;
          throw;
          }
          finally
          {
          oSQLServer.DisConnect();
          }
          }
          /// <summary>
          /// 數據庫恢復
          /// </summary>
          public string DbRestore(string url)
          {
          if(exepro()!=true)//執行存儲過程
          {
          return "操作失敗";
          }
          else
          {
          SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
          SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
          try
          {
          oSQLServer.LoginSecure = false;
          oSQLServer.Connect(server, uid, pwd);
          oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
          oRestore.Database = database;
          oRestore.Files = url;//@"d:/Northwind.bak";
          oRestore.FileNumber = 1;
          oRestore.ReplaceDatabase = true;
          oRestore.SQLRestore(oSQLServer);
          return "ok";
          }
          catch(Exception e)
          {
          return "恢復數據庫失敗";
          throw;
          }
          finally
          {
          oSQLServer.DisConnect();
          }
          }
          }
          private bool exepro()
          {
          SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");
          SqlCommand cmd = new SqlCommand("killspid",conn1);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Add("@dbname","port");
          try
          {
          conn1.Open();
          cmd.ExecuteNonQuery();
          return true;
          }
          catch(Exception ex)
          {
          return false;
          }
          finally
          {
          conn1.Close();
          }
          }
          }
          }

          posted on 2014-05-08 16:47 順其自然EVO 閱讀(136) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 滕州市| 中宁县| 乌鲁木齐市| 嵊泗县| 巨野县| 福清市| 特克斯县| 山西省| 漯河市| 嘉定区| 临桂县| 三穗县| 云林县| 伊春市| 吉水县| 新巴尔虎右旗| 峨眉山市| 布拖县| 砚山县| 嘉祥县| 株洲市| 新巴尔虎右旗| 潮州市| 富阳市| 松原市| 玛纳斯县| 邢台县| 惠东县| 弋阳县| 墨脱县| 甘洛县| 新沂市| 台江县| 繁峙县| 怀集县| 大足县| 乐都县| 专栏| 抚远县| 郴州市| 梁河县|