首页 > 代码库 > 用asp.net还原与恢复sqlserver数据库
用asp.net还原与恢复sqlserver数据库
上次做了个项目,涉及到数据库的还原和恢复,到网上找了一下,是利用SQLDMO实现的,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。
我把原作者的类扩充了一下,可以自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。
需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:
create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500)
declare @spid int
set @sql=‘declare getspid cursor for
select spid from sysprocesses where dbid=db_id(‘‘‘+@dbname+‘‘‘)‘
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
exec(‘kill ‘+@spid)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
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(); } } } }
<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399314639&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3709219.html&dt=1399314643067&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740&correlator=1399314642684&frm=20&ga_vid=429972749.1397695120&ga_sid=1399310453&ga_hid=1460242815&ga_fc=1&u_tz=480&u_his=138&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=61853&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=8Y7V4uOG5q&p=http%3A//www.cnblogs.com&dtd=101" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399314639&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3709219.html&dt=1399314643175&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740%2C8660799060&correlator=1399314642684&frm=20&ga_vid=429972749.1397695120&ga_sid=1399310453&ga_hid=1460242815&ga_fc=1&u_tz=480&u_his=138&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=62103&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=lpnUL8dq2i&p=http%3A//www.cnblogs.com&dtd=41" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。