|
備份:
在備份按鈕里寫(xiě):
復(fù)制代碼 代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string path = "e://MAZ數(shù)據(jù)庫(kù)備份//" + Menu+ ".bak";
if (File.Exists(path))
{
File.Delete(path);//注意,這個(gè)步驟很重要,如果重復(fù),在備份的數(shù)據(jù),就會(huì)變成,
//你剛開(kāi)始的數(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();
}
}
還原:
在還原按鈕里寫(xiě):
復(fù)制代碼 代碼如下:
protected void Button2_Click(object sender, EventArgs e)
{
string path = "e://MAZ數(shù)據(jù)庫(kù)備份//" + 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ǔ)過(guò)程 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
AspNet技術(shù):asp.net 因?yàn)閿?shù)據(jù)庫(kù)正在使用的解決方法,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。