/***********************************************************
**编 号:SC20090702-001
**作 者:zhpingyun008@qq.com
**注释说明:强制中断数据库连接,用于数据库还原操作
***********************************************************/
use master
go
create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500),@temp varchar(1000)
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 =0
begin
set @temp='kill '+rtrim(@spid)
exec(@temp)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
/***********************************************************
**编 号:CJ20090702-001
**作 者:zhpingyun008@qq.com
**注释说明:数据库备份操作
***********************************************************/
protected void Button1_Click1(object sender, EventArgs e)
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(".", "sa", "zhpingyun");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = "CRM";
oBackup.Files = @"D:\CRM.bak";
oBackup.BackupSetName = "CRM";
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
Response.Write("<script>alert('数据库备份成功!');</script>");
}
catch (Exception ex)
{
throw ex;
Response.Write("<script>alert('数据库备份失败!');</script>");
}
finally
{
oSQLServer.DisConnect();
}
}
/***********************************************************
**编 号:CJ20090702-002
**作 者:zhpingyun008@qq.com
**注释说明:数据库还原操作
***********************************************************/
protected void Button2_Click(object sender, EventArgs e)
{
if (exepro() != true)//执行存储过程
{
Response.Write("<script>alert('存储过程killspid执行失败!');</script>");
}
else
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(".", "sa", "zhpingyun");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "CRM";
oRestore.Files = @"D:\CRM.bak";
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
Response.Write("<script>alert('数据还原成功!');</script>");
}
catch (Exception ex)
{
throw ex;
Response.Write("<script>alert('数据还原失败!');</script>");
}
finally
{
oSQLServer.DisConnect();
}
}
}
/***********************************************************
**编 号:CJ20090702-003
**作 者:zhpingyun008@qq.com
**注释说明:把正在连接中的用户都中断,然后才能还原
***********************************************************/
private bool exepro()
{
SqlConnection conn1 = new SqlConnection("server=.;uid=sa;pwd=zhpingyun;database=master");
SqlCommand cmd = new SqlCommand("killspid", conn1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dbname", "CRM");
try
{
conn1.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
conn1.Close();
}
}
use master
exec killspid 'test'
C#数据库备份及还原的实现代码
4星 · 超过85%的资源 需积分: 10 37 浏览量
2010-03-19
15:35:27
上传
评论 1
收藏 4KB RAR 举报
txq_hd
- 粉丝: 2
- 资源: 14
最新资源
- 基于python开发的口红色号识别程序+源码+开发文档+源码解析(毕业设计&课程设计&项目开发)
- TP-LINK TL-WN725N V3 Linux 驱动
- 020ssm-jsp-mysql班级同学录网站.zip(可运行源码+数据库文件+文档)
- 什么是stm32f103rct6,有哪些优缺点?
- 李明哲尚能2.zip
- 019ssm-jsp-mysql奥迪维修保养服务管理系统.zip(可运行源码+数据库文件+)
- AB测试数据-增设中小店铺广告位
- YOLOv8红外场景的车辆-行人-斑马线-交通灯检测+数据集+pyqt界面
- 基于JSP毕业设计-OA办公自动化系统-毕业设计.zip
- 基于JSP毕业设计-MVC设计模式应用之游戏卡在线销售系统(论文).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈