用asp.net还原与恢复sqlserver数据库(转)

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:
None.gif利用SQLDMO实现的,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。
None.gif我把原作者的类扩充了一下,可以自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。
None.gif
None.gif需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:
None.gif
None.gif
None.gifcreate proc killspid (@dbname varchar(20))
None.gif as
None.gifbegin
None.gifdeclare @sql nvarchar(500)
None.gifdeclare @spid int
None.gif set @sql='declare getspid cursor for
None.gifselect spid from sysprocesses where dbid=db_id('''+@dbname+''')'
None.gifexec (@sql)
None.gifopen getspid
None.giffetch next from getspid into @spid
None.gif while @@fetch_status<>-1
None.gifbegin
None.gifexec('kill '+@spid)
None.giffetch next from getspid into @spid
None.gifend
None.gifclose getspid
None.gifdeallocate getspid
None.gifend
None.gifGO
None.gif
None.gif
None.gif在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)
None.gif
None.gif
None.gif using System;
None.gif
None.gif using System.Configuration;
None.gif
None.gif using System.Data.SqlClient;
None.gif
None.gif using System.Data;
None.gif
None.gif namespace web.base_class
None.gif
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
InBlock.gif
/// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gif
InBlock.gifpublic class DbOper
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifprivate string server;
InBlock.gif
InBlock.gifprivate string uid;
InBlock.gif
InBlock.gifprivate string pwd;
InBlock.gif
InBlock.gifprivate string database;
InBlock.gif
InBlock.gifprivate string conn;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
InBlock.gif
/// DbOper类的构造函数
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gif
InBlock.gifpublic DbOper()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
InBlock.gif
InBlock.gif server=cut(conn,"server=",";");
InBlock.gif
InBlock.gif uid=cut(conn,"uid=",";");
InBlock.gif
InBlock.gif pwd=cut(conn,"pwd=",";");
InBlock.gif
InBlock.gif database=cut(conn,"database=",";");
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublic string cut(string str,string bg,string ed)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifstring sub;
InBlock.gif
InBlock.gif sub=str.Substring(str.IndexOf(bg)+bg.Length);
InBlock.gif
InBlock.gif sub=sub.Substring(0,sub.IndexOf(";"));
InBlock.gif
InBlock.gifreturn sub;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
InBlock.gif
/// 数据库备份
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gif
InBlock.gifpublic bool DbBackup(string url)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
InBlock.gif
InBlock.gif SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
InBlock.gif
InBlock.giftry
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif oSQLServer.LoginSecure = false;
InBlock.gif
InBlock.gif oSQLServer.Connect(server,uid, pwd);
InBlock.gif
InBlock.gif oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
InBlock.gif
InBlock.gif oBackup.Database = database;
InBlock.gif
InBlock.gif oBackup.Files = url;//"d:\Northwind.bak";
InBlock.gif

InBlock.gif oBackup.BackupSetName = database;
InBlock.gif
InBlock.gif oBackup.BackupSetDescription = "数据库备份";
InBlock.gif
InBlock.gif oBackup.Initialize = true;
InBlock.gif
InBlock.gif oBackup.SQLBackup(oSQLServer);
InBlock.gif
InBlock.gifreturn true;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifcatch
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifreturn false;
InBlock.gif
InBlock.gifthrow;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.giffinally
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif oSQLServer.DisConnect();
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
InBlock.gif
/// 数据库恢复
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gif
InBlock.gifpublic string DbRestore(string url)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifif(exepro()!=true)//执行存储过程
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifreturn "操作失败";
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifelse
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
InBlock.gif
InBlock.gif SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
InBlock.gif
InBlock.giftry
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif oSQLServer.LoginSecure = false;
InBlock.gif
InBlock.gif oSQLServer.Connect(server, uid, pwd);
InBlock.gif
InBlock.gif oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
InBlock.gif
InBlock.gif oRestore.Database = database;
InBlock.gif
InBlock.gif oRestore.Files = url;//@"d:\Northwind.bak";
InBlock.gif

InBlock.gif oRestore.FileNumber = 1;
InBlock.gif
InBlock.gif oRestore.ReplaceDatabase = true;
InBlock.gif
InBlock.gif oRestore.SQLRestore(oSQLServer);
InBlock.gif
InBlock.gifreturn "ok";
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifcatch(Exception e)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifreturn "恢复数据库失败";
InBlock.gif
InBlock.gifthrow;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.giffinally
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif oSQLServer.DisConnect();
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifprivate bool exepro()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");
InBlock.gif
InBlock.gif SqlCommand cmd = new SqlCommand("killspid",conn1);
InBlock.gif
InBlock.gif cmd.CommandType = CommandType.StoredProcedure;
InBlock.gif
InBlock.gif cmd.Parameters.Add("@dbname","port");
InBlock.gif
InBlock.giftry
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif conn1.Open();
InBlock.gif
InBlock.gif cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gifreturn true;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifcatch(Exception ex)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifreturn false;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.giffinally
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif conn1.Close();
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif



本文转自高海东博客园博客,原文链接http://www.cnblogs.com/ghd258/archive/2006/02/28/339458.html,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
7天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
45 10
|
7天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
62 6
|
7天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
|
28天前
|
SQL Oracle 关系型数据库
干货!sqlserver数据库所有知识点总结整理,含代码(挺全的)
干货!sqlserver数据库所有知识点总结整理,含代码(挺全的)
11 0
|
15天前
|
SQL 数据可视化 关系型数据库
轻松入门MySQL:深入探究MySQL的ER模型,数据库设计的利器与挑战(22)
轻松入门MySQL:深入探究MySQL的ER模型,数据库设计的利器与挑战(22)
|
15天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
|
15天前
|
关系型数据库 MySQL 数据库
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
|
17天前
|
存储 关系型数据库 MySQL
数据库字符编码MySQL中使用UTF-8还是UTFB4
数据库字符编码MySQL中使用UTF-8还是UTFB4
20 0
|
20天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
93 0
|
11天前
|
存储 关系型数据库 MySQL
MySQL基础入门:数据库操作全攻略
MySQL基础入门:数据库操作全攻略
44 0