首页 > 编程 > .NET > 正文

用ASP.NET还原与恢复SQL Server数据库

2024-07-10 13:03:00
字体:
来源:转载
供稿:网友

需要注意的是还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在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('''[email protected]+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
exec('kill '[email protected])
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;

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表