发布于 2016-05-29 23:44:56 | 84 次阅读 | 评论: 1 | 来源: 网友投递
ASP.NET
ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。
protected void Button1_Click(object sender, EventArgs e)
{
string path = "e:\\MAZ数据库备份\\" + Menu+ ".bak";
if (File.Exists(path))
{
File.Delete(path);//注意,这个步骤很重要,如果重复,在备份的数据,就会变成,
//你刚开始的数据,所以每次都要先删除.
}
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数据库备份\\" + 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+ "'";//这个很关键,要不然就出现题目上的错误了
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();
}
}
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