asp.net 因?yàn)閿?shù)據(jù)庫正在使用的解決方法
更新時(shí)間:2009年02月20日 02:01:19 作者:
因?yàn)閿?shù)據(jù)庫正在使用,所以未能獲得對(duì)數(shù)據(jù)庫的排它訪問權(quán)?
這個(gè)問題困惑我好長(zhǎng)的時(shí)間,在網(wǎng)上搜,也沒完全的解決方案,不是過于簡(jiǎn)單,就是亂說,有的論壇上還沒人回答這個(gè)問題.今天我徹底解決這個(gè)問題,并在C#里測(cè)試完全通過.現(xiàn)在把他寫出來,希望對(duì)朋友們有幫助(如要轉(zhuǎn)載,記得給我版權(quán)哦.嘿嘿?。。。韵滦畔⑹蔷C合網(wǎng)上的資料和我的實(shí)際問題,整理出來的.
備份:
在備份按鈕里寫:
protected void Button1_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ數(shù)據(jù)庫備份\\" + Menu+ ".bak";
if (File.Exists(path))
{
File.Delete(path);//注意,這個(gè)步驟很重要,如果重復(fù),在備份的數(shù)據(jù),就會(huì)變成,
//你剛開始的數(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+ "'";//這個(gè)很關(guān)鍵,要不然就出現(xiàn)題目上的錯(cuò)誤了
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand(backupstr, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("恢復(fù)成功!");
connection.Close();
}
catch (Exception ex)
{
string stringError = ex.ToString();
MessageBox.Show("恢復(fù)失??!");
connection.Close();
}
}
存儲(chǔ)過程 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
備份:
在備份按鈕里寫:
復(fù)制代碼 代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ數(shù)據(jù)庫備份\\" + Menu+ ".bak";
if (File.Exists(path))
{
File.Delete(path);//注意,這個(gè)步驟很重要,如果重復(fù),在備份的數(shù)據(jù),就會(huì)變成,
//你剛開始的數(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();
}
}
還原:
在還原按鈕里寫:
復(fù)制代碼 代碼如下:
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+ "'";//這個(gè)很關(guān)鍵,要不然就出現(xiàn)題目上的錯(cuò)誤了
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand(backupstr, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("恢復(fù)成功!");
connection.Close();
}
catch (Exception ex)
{
string stringError = ex.ToString();
MessageBox.Show("恢復(fù)失??!");
connection.Close();
}
}
存儲(chǔ)過程 killspid
復(fù)制代碼 代碼如下:
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)文章
.NET?Core配置TLS?Cipher(套件)的詳細(xì)過程
本文以.NET?5為例,只不過針對(duì).NET?Core?3或3.1通過工具掃描出的協(xié)議套件結(jié)果略有所差異,但不影響我們對(duì)安全套件的配置,我們使用OpenSSL生成自簽名證書,對(duì).NET?Core配置TLS?Cipher相關(guān)知識(shí)感興趣的朋友一起看看吧2021-12-12asp.net Javascript獲取CheckBoxList的value
最近在做一個(gè)BS的小項(xiàng)目,記得自己搞asp.net的時(shí)候,還是兩年以前,大部分的東西只是有點(diǎn)印象,忘得差不多了,所以這次也算是溫習(xí)的過程吧,一邊學(xué)習(xí),一邊趕工,呵呵呵。。。。2009-12-12asp.net html控件的File控件實(shí)現(xiàn)多文件上傳實(shí)例分享
asp.net中html控件的File控件實(shí)現(xiàn)多文件上傳簡(jiǎn)單實(shí)例,開發(fā)工具vs2010使用c#語言,感興趣的朋友可以了解下,必定是多文件上傳值得學(xué)習(xí),或許本文所提供的知識(shí)點(diǎn)對(duì)你有所幫助2013-02-02xUnit 編寫 ASP.NET Core 單元測(cè)試的方法
這篇文章主要介紹了xUnit 編寫 ASP.NET Core 單元測(cè)試的方法,文中代碼非常詳細(xì),幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06ASP.NET中使用開源組件NPOI快速導(dǎo)入導(dǎo)出Execl數(shù)據(jù)
這篇文章主要介紹了ASP.NET中使用開源組件NPOI快速導(dǎo)入導(dǎo)出Execl數(shù)據(jù),NPOI是一個(gè)很強(qiáng)大的Execl操作組件,需要的朋友可以參考下2014-09-09