·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » 用asp.net还原与恢复sqlserver数据库

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

类别: ASP.NET教程  评论数:0 总得分:0
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();

              }

 

         }

     }

}

-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1