1
利用SQLDMO实现的,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。
2
我把原作者的类扩充了一下,可以自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。
3
4
需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:
5
6
7
create proc killspid (@dbname varchar(20))
8
as
9
begin
10
declare @sql nvarchar(500)
11
declare @spid int
12
set @sql='declare getspid cursor for
13
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
14
exec (@sql)
15
open getspid
16
fetch next from getspid into @spid
17
while @@fetch_status<>-1
18
begin
19
exec('kill '+@spid)
20
fetch next from getspid into @spid
21
end
22
close getspid
23
deallocate getspid
24
end
25
GO
26
27
28
在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)
29
30
31
using System;
32
33
using System.Configuration;
34
35
using System.Data.SqlClient;
36
37
using System.Data;
38
39
namespace web.base_class
40
41
{
42
43
/**//// <summary>
44
45
/// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
46
47
/// </summary>
48
49
public class DbOper
50
51
{
52
53
private string server;
54
55
private string uid;
56
57
private string pwd;
58
59
private string database;
60
61
private string conn;
62
63
/**//// <summary>
64
65
/// DbOper类的构造函数
66
67
/// </summary>
68
69
public DbOper()
70
71
{
72
73
conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
74
75
server=cut(conn,"server=",";");
76
77
uid=cut(conn,"uid=",";");
78
79
pwd=cut(conn,"pwd=",";");
80
81
database=cut(conn,"database=",";");
82
83
}
84
85
public string cut(string str,string bg,string ed)
86
87
{
88
89
string sub;
90
91
sub=str.Substring(str.IndexOf(bg)+bg.Length);
92
93
sub=sub.Substring(0,sub.IndexOf(";"));
94
95
return sub;
96
97
}
98
99
100
101
/**//// <summary>
102
103
/// 数据库备份
104
105
/// </summary>
106
107
public bool DbBackup(string url)
108
109
{
110
111
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
112
113
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
114
115
try
116
117
{
118
119
oSQLServer.LoginSecure = false;
120
121
oSQLServer.Connect(server,uid, pwd);
122
123
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
124
125
oBackup.Database = database;
126
127
oBackup.Files = url;//"d:\Northwind.bak";
128
129
oBackup.BackupSetName = database;
130
131
oBackup.BackupSetDescription = "数据库备份";
132
133
oBackup.Initialize = true;
134
135
oBackup.SQLBackup(oSQLServer);
136
137
return true;
138
139
}
140
141
catch
142
143
{
144
145
return false;
146
147
throw;
148
149
}
150
151
finally
152
153
{
154
155
oSQLServer.DisConnect();
156
157
}
158
159
}
160
161
162
163
/**//// <summary>
164
165
/// 数据库恢复
166
167
/// </summary>
168
169
public string DbRestore(string url)
170
171
{
172
173
if(exepro()!=true)//执行存储过程
174
175
{
176
177
return "操作失败";
178
179
}
180
181
else
182
183
{
184
185
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
186
187
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
188
189
try
190
191
{
192
193
oSQLServer.LoginSecure = false;
194
195
oSQLServer.Connect(server, uid, pwd);
196
197
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
198
199
oRestore.Database = database;
200
201
oRestore.Files = url;//@"d:\Northwind.bak";
202
203
oRestore.FileNumber = 1;
204
205
oRestore.ReplaceDatabase = true;
206
207
oRestore.SQLRestore(oSQLServer);
208
209
return "ok";
210
211
}
212
213
catch(Exception e)
214
215
{
216
217
return "恢复数据库失败";
218
219
throw;
220
221
}
222
223
finally
224
225
{
226
227
oSQLServer.DisConnect();
228
229
}
230
231
}
232
233
}
234
235
private bool exepro()
236
237
{
238
239
SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");
240
241
SqlCommand cmd = new SqlCommand("killspid",conn1);
242
243
cmd.CommandType = CommandType.StoredProcedure;
244
245
cmd.Parameters.Add("@dbname","port");
246
247
try
248
249
{
250
251
conn1.Open();
252
253
cmd.ExecuteNonQuery();
254
255
return true;
256
257
}
258
259
catch(Exception ex)
260
261
{
262
263
return false;
264
265
}
266
267
finally
268
269
{
270
271
conn1.Close();
272
273
}
274
275
276
277
}
278
279
}
280
281
}
282
利用SQLDMO实现的,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。2
我把原作者的类扩充了一下,可以自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。3

4
需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:5

6

7
create proc killspid (@dbname varchar(20))8
as9
begin10
declare @sql nvarchar(500)11
declare @spid int12
set @sql='declare getspid cursor for 13
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'14
exec (@sql)15
open getspid16
fetch next from getspid into @spid17
while @@fetch_status<>-118
begin19
exec('kill '+@spid)20
fetch next from getspid into @spid21
end22
close getspid23
deallocate getspid24
end25
GO26

27

28
在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)29
30

31
using System;32

33
using System.Configuration;34

35
using System.Data.SqlClient;36

37
using System.Data;38

39
namespace web.base_class40

41
{42

43
/**//// <summary>44

45
/// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复46

47
/// </summary>48

49
public class DbOper50

51
{52

53
private string server;54

55
private string uid;56

57
private string pwd;58

59
private string database;60

61
private string conn;62

63
/**//// <summary>64

65
/// DbOper类的构造函数66

67
/// </summary>68

69
public DbOper()70

71
{72

73
conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();74

75
server=cut(conn,"server=",";");76

77
uid=cut(conn,"uid=",";");78

79
pwd=cut(conn,"pwd=",";");80

81
database=cut(conn,"database=",";");82

83
}84

85
public string cut(string str,string bg,string ed)86

87
{88

89
string sub;90

91
sub=str.Substring(str.IndexOf(bg)+bg.Length);92

93
sub=sub.Substring(0,sub.IndexOf(";"));94

95
return sub;96

97
}98

99
100

101
/**//// <summary>102

103
/// 数据库备份104

105
/// </summary>106

107
public bool DbBackup(string url)108

109
{110

111
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();112

113
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();114

115
try116

117
{118

119
oSQLServer.LoginSecure = false;120

121
oSQLServer.Connect(server,uid, pwd);122

123
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;124

125
oBackup.Database = database;126

127
oBackup.Files = url;//"d:\Northwind.bak";128

129
oBackup.BackupSetName = database;130

131
oBackup.BackupSetDescription = "数据库备份";132

133
oBackup.Initialize = true;134

135
oBackup.SQLBackup(oSQLServer);136

137
return true;138

139
}140

141
catch142

143
{144

145
return false;146

147
throw;148

149
}150

151
finally152

153
{154

155
oSQLServer.DisConnect();156

157
}158

159
}160

161
162

163
/**//// <summary>164

165
/// 数据库恢复166

167
/// </summary>168

169
public string DbRestore(string url)170

171
{172

173
if(exepro()!=true)//执行存储过程174

175
{176

177
return "操作失败";178

179
}180

181
else182

183
{184

185
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();186

187
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();188

189
try190

191
{192

193
oSQLServer.LoginSecure = false;194

195
oSQLServer.Connect(server, uid, pwd);196

197
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;198

199
oRestore.Database = database;200

201
oRestore.Files = url;//@"d:\Northwind.bak";202

203
oRestore.FileNumber = 1;204

205
oRestore.ReplaceDatabase = true;206

207
oRestore.SQLRestore(oSQLServer);208

209
return "ok";210

211
}212

213
catch(Exception e)214

215
{216

217
return "恢复数据库失败";218

219
throw;220

221
}222

223
finally224

225
{226

227
oSQLServer.DisConnect();228

229
}230

231
}232

233
}234

235
private bool exepro()236

237
{238

239
SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");240

241
SqlCommand cmd = new SqlCommand("killspid",conn1);242

243
cmd.CommandType = CommandType.StoredProcedure;244

245
cmd.Parameters.Add("@dbname","port");246

247
try248

249
{250

251
conn1.Open();252

253
cmd.ExecuteNonQuery();254

255
return true;256

257
}258

259
catch(Exception ex)260

261
{262

263
return false;264

265
}266

267
finally268

269
{270

271
conn1.Close();272

273
}274

275
276

277
}278

279
}280

281
}282




浙公网安备 33010602011771号