asp.net 因為數(shù)據(jù)庫正在使用的解決方法
更新時間:2009年02月20日 02:01:19 作者:
因為數(shù)據(jù)庫正在使用,所以未能獲得對數(shù)據(jù)庫的排它訪問權(quán)?
這個問題困惑我好長的時間,在網(wǎng)上搜,也沒完全的解決方案,不是過于簡單,就是亂說,有的論壇上還沒人回答這個問題.今天我徹底解決這個問題,并在C#里測試完全通過.現(xiàn)在把他寫出來,希望對朋友們有幫助(如要轉(zhuǎn)載,記得給我版權(quán)哦.嘿嘿?。。。韵滦畔⑹蔷C合網(wǎng)上的資料和我的實際問題,整理出來的.
備份:
在備份按鈕里寫:
protected void Button1_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ數(shù)據(jù)庫備份\\" + Menu+ ".bak";
if (File.Exists(path))
{
File.Delete(path);//注意,這個步驟很重要,如果重復,在備份的數(shù)據(jù),就會變成,
//你剛開始的數(shù)據(jù),所以每次都要先刪除.
}
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
string backupstr="backup database Test to disk='"+path+"';";
SqlConnection con = new SqlConnection("server=localhost;database=Menu;uid=sa;pwd=sa;");
SqlCommand cmd = new SqlCommand(backupstr, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("備份成功!");
connection.Close();
}
catch (Exception ex)
{
string stringError = ex.ToString();
MessageBox.Show("備份失??!");
connection.Close();
}
}
還原:
在還原按鈕里寫:
protected void Button2_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ數(shù)據(jù)庫備份\\" + Menu+ ".bak";
string connectionStringTest = "server=localhost ;database=master;uid=sa;pwd=sa";
SqlConnection connection = new SqlConnection(connectionStringTest);
string backupstr = "restore database Menu from disk='" + path + "';";
try
{
string sql = "exec killspid '" + Menu+ "'";//這個很關(guān)鍵,要不然就出現(xiàn)題目上的錯誤了
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand(backupstr, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("恢復成功!");
connection.Close();
}
catch (Exception ex)
{
string stringError = ex.ToString();
MessageBox.Show("恢復失??!");
connection.Close();
}
}
存儲過程 killspid
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
備份:
在備份按鈕里寫:
復制代碼 代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ數(shù)據(jù)庫備份\\" + Menu+ ".bak";
if (File.Exists(path))
{
File.Delete(path);//注意,這個步驟很重要,如果重復,在備份的數(shù)據(jù),就會變成,
//你剛開始的數(shù)據(jù),所以每次都要先刪除.
}
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
string backupstr="backup database Test to disk='"+path+"';";
SqlConnection con = new SqlConnection("server=localhost;database=Menu;uid=sa;pwd=sa;");
SqlCommand cmd = new SqlCommand(backupstr, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("備份成功!");
connection.Close();
}
catch (Exception ex)
{
string stringError = ex.ToString();
MessageBox.Show("備份失??!");
connection.Close();
}
}
還原:
在還原按鈕里寫:
復制代碼 代碼如下:
protected void Button2_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ數(shù)據(jù)庫備份\\" + Menu+ ".bak";
string connectionStringTest = "server=localhost ;database=master;uid=sa;pwd=sa";
SqlConnection connection = new SqlConnection(connectionStringTest);
string backupstr = "restore database Menu from disk='" + path + "';";
try
{
string sql = "exec killspid '" + Menu+ "'";//這個很關(guān)鍵,要不然就出現(xiàn)題目上的錯誤了
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand(backupstr, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("恢復成功!");
connection.Close();
}
catch (Exception ex)
{
string stringError = ex.ToString();
MessageBox.Show("恢復失??!");
connection.Close();
}
}
存儲過程 killspid
復制代碼 代碼如下:
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
相關(guān)文章
asp.net使用母版頁中使用ajax腳本取數(shù)據(jù)
因母版頁繼承自UserControl,我們無法像正常頁面那樣使用Jquey或Ajax的PageMethods等無刷新方法取數(shù)據(jù)。不過可以使用ajax提供的Sys.Net.WebRequest來解決這一問題。2010-09-09
IIS7偽靜態(tài)web.config配置的方法和規(guī)則
本文主要介紹IIS7上配置偽靜態(tài)的超簡單的新方法,安裝URLRewrite插件,配置web.config即可。2016-04-04
Asp.net TextBox的TextChanged事件使用介紹
動態(tài)創(chuàng)建的控件是如何加載視圖狀態(tài),還提到ProcessPostData方法的調(diào)用,這里我就用TextBox的TextChanged事件來說說視圖數(shù)據(jù)的加載以及事件的觸發(fā)2012-12-12
C# 實現(xiàn)抓取網(wǎng)站頁面內(nèi)容的實例方法
這篇文章介紹了C# 實現(xiàn)抓取網(wǎng)站頁面內(nèi)容的實例方法,有需要的朋友可以參考一下2013-08-08

