用asp.net还原与恢复sqlserver数据库
利用SQLDMO实现,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。
自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。
需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:
在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)
利用SQLDMO实现,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。
自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。
需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:
 1
create proc killspid (@dbname varchar(20))
2
as
3
begin
4
    declare @sql nvarchar(500)
5
    declare @spid int
6
    set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
7
    exec (@sql)
8
    open getspid
9
    fetch next from getspid into @spid
10
    while @@fetch_status<>-1
11
    begin
12
    exec('kill '+@spid)
13
    fetch next from getspid into @spid
14
    end
15
    close getspid
16
    deallocate getspid
17
end
18
GO
create proc killspid (@dbname varchar(20))2
as3
begin4
    declare @sql nvarchar(500)5
    declare @spid int6
    set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')'7
    exec (@sql)8
    open getspid9
    fetch next from getspid into @spid10
    while @@fetch_status<>-111
    begin12
    exec('kill '+@spid)13
    fetch next from getspid into @spid14
    end15
    close getspid16
    deallocate getspid17
end18
GO在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)
  1
using System;
2
using System.Configuration;
3
using System.Data.SqlClient;
4
using System.Data;
5
namespace web.base_class
6
{
7
     /// <summary>
8
     /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
9
     /// </summary>
10
     public class DbOper
11
     {
12
          private string server;
13
          private string uid;
14
          private string pwd;
15
          private string database;
16
          private string conn;
17
         /// <summary>
18
         /// DbOper类的构造函数
19
         /// </summary>
20
         public DbOper()
21
         {
22
              conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
23
              server=cut(conn,"server=",";");
24
              uid=cut(conn,"uid=",";");
25
              pwd=cut(conn,"pwd=",";");
26
              database=cut(conn,"database=",";");
27
         }
28
         public string cut(string str,string bg,string ed)
29
         {
30
              string sub;
31
              sub=str.Substring(str.IndexOf(bg)+bg.Length);
32
              sub=sub.Substring(0,sub.IndexOf(";"));
33
              return sub;
34
         }
35
 
36
         /// <summary>
37
         /// 数据库备份
38
         /// </summary>
39
         public  bool DbBackup(string url)
40
         {
41
              SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
42
              SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
43
              try
44
              {
45
                   oSQLServer.LoginSecure = false;
46
                   oSQLServer.Connect(server,uid, pwd);
47
                   oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
48
                   oBackup.Database = database;
49
                   oBackup.Files = url;//"d:\Northwind.bak";
50
                   oBackup.BackupSetName = database;
51
                   oBackup.BackupSetDescription = "数据库备份";
52
                   oBackup.Initialize = true;
53
                   oBackup.SQLBackup(oSQLServer);
54
                   return true;
55
              }
56
              catch
57
              {
58
                   return false;
59
                   throw;
60
              }
61
              finally
62
              {
63
                   oSQLServer.DisConnect();
64
              }
65
         }
66
 
67
         /// <summary>
68
         /// 数据库恢复
69
         /// </summary>
70
         public string DbRestore(string url)
71
         {
72
              if(exepro()!=true)//执行存储过程
73
              {
74
                   return "操作失败";
75
              }
76
              else
77
              {
78
                   SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
79
                   SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
80
                   try
81
                   {
82
                        oSQLServer.LoginSecure = false;
83
                        oSQLServer.Connect(server, uid, pwd);
84
                        oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
85
                        oRestore.Database = database;
86
                        oRestore.Files = url;//@"d:\Northwind.bak";
87
                        oRestore.FileNumber = 1;
88
                        oRestore.ReplaceDatabase = true;
89
                        oRestore.SQLRestore(oSQLServer);
90
                       return "ok";
91
                   }
92
                   catch(Exception e)
93
                   {
94
                       return "恢复数据库失败";
95
                       throw;
96
                   }
97
                   finally
98
                   {
99
                        oSQLServer.DisConnect();
100
                   }
101
              }
102
         }
103
          private bool exepro()
104
         {
105
              SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");
106
              SqlCommand cmd = new SqlCommand("killspid",conn1);
107
              cmd.CommandType = CommandType.StoredProcedure;
108
              cmd.Parameters.Add("@dbname","port");
109
              try
110
              {
111
                   conn1.Open();
112
                   cmd.ExecuteNonQuery();
113
                   return true;
114
              }
115
              catch(Exception ex)
116
              {
117
                   return false;
118
              }
119
              finally
120
              {
121
                   conn1.Close();
122
              }
123
 
124
         }
125
     }
126
}
using System;2
using System.Configuration;3
using System.Data.SqlClient;4
using System.Data;5
namespace web.base_class6
{7
     /// <summary>8
     /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复9
     /// </summary>10
     public class DbOper11
     {12
          private string server;13
          private string uid;14
          private string pwd;15
          private string database;16
          private string conn;17
         /// <summary>18
         /// DbOper类的构造函数19
         /// </summary>20
         public DbOper()21
         {22
              conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();23
              server=cut(conn,"server=",";");24
              uid=cut(conn,"uid=",";");25
              pwd=cut(conn,"pwd=",";");26
              database=cut(conn,"database=",";");27
         }28
         public string cut(string str,string bg,string ed)29
         {30
              string sub;31
              sub=str.Substring(str.IndexOf(bg)+bg.Length);32
              sub=sub.Substring(0,sub.IndexOf(";"));33
              return sub;34
         }35
 36
         /// <summary>37
         /// 数据库备份38
         /// </summary>39
         public  bool DbBackup(string url)40
         {41
              SQLDMO.Backup oBackup = new SQLDMO.BackupClass();42
              SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();43
              try44
              {45
                   oSQLServer.LoginSecure = false;46
                   oSQLServer.Connect(server,uid, pwd);47
                   oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;48
                   oBackup.Database = database;49
                   oBackup.Files = url;//"d:\Northwind.bak";50
                   oBackup.BackupSetName = database;51
                   oBackup.BackupSetDescription = "数据库备份";52
                   oBackup.Initialize = true;53
                   oBackup.SQLBackup(oSQLServer);54
                   return true;55
              }56
              catch57
              {58
                   return false;59
                   throw;60
              }61
              finally62
              {63
                   oSQLServer.DisConnect();64
              }65
         }66
 67
         /// <summary>68
         /// 数据库恢复69
         /// </summary>70
         public string DbRestore(string url)71
         {72
              if(exepro()!=true)//执行存储过程73
              {74
                   return "操作失败";75
              }76
              else77
              {78
                   SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();79
                   SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();80
                   try81
                   {82
                        oSQLServer.LoginSecure = false;83
                        oSQLServer.Connect(server, uid, pwd);84
                        oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;85
                        oRestore.Database = database;86
                        oRestore.Files = url;//@"d:\Northwind.bak";87
                        oRestore.FileNumber = 1;88
                        oRestore.ReplaceDatabase = true;89
                        oRestore.SQLRestore(oSQLServer);90
                       return "ok";91
                   }92
                   catch(Exception e)93
                   {94
                       return "恢复数据库失败";95
                       throw;96
                   }97
                   finally98
                   {99
                        oSQLServer.DisConnect();100
                   }101
              }102
         }103
          private bool exepro()104
         {105
              SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");106
              SqlCommand cmd = new SqlCommand("killspid",conn1);107
              cmd.CommandType = CommandType.StoredProcedure;108
              cmd.Parameters.Add("@dbname","port");109
              try110
              {111
                   conn1.Open();112
                   cmd.ExecuteNonQuery();113
                   return true;114
              }115
              catch(Exception ex)116
              {117
                   return false;118
              }119
              finally120
              {121
                   conn1.Close();122
              }123
 124
         }125
     }126
}
                    
                



     
                
            
        
浙公网安备 33010602011771号