用ASP.NET還原與恢複SQL Server資料庫

來源:互聯網
上載者:User
需要注意的時還原,還原的時候問題最大了,有別的使用者使用資料庫的時候無法還原,解決辦法就是在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>

/// Database Backup

/// </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 = "Database Backup";

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();

}

}

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.