首页 > 资讯列表 > 编程/数据库 >> ASP/.NET

ASP.NET备份恢复SqlServer数据库

ASP/.NET 2011-05-18 00:05:30 转载来源: AS网站目录整理

ASP.NET备份恢复SqlServer数据库

备份SqlServer数据库:
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "backup database " + this.DropDownList1.SelectedValue + " to disk='" + this.TextBox1.Text.Trim() + ".bak'";
SqlConnection con = new SqlConnection(SqlStr1);
con.Open();
try
{
if (File.Exists(this.TextBox1.Text.Trim()))
{
Response.Write("<script language=javascript>alert('此文件已存在,请从新输入!');location='Default.aspx'</script>");
return;
}
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('备份数据成功!');location='Default.aspx'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('备份数据失败!')</script>");
}
finally
{
con.Close();
}
还原SqlServer数据库:
string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
string dbname = this.DropDownList1.SelectedValue;
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";
SqlConnection con = new SqlConnection(SqlStr1);
con.Open();
try
{
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('还原数据成功!');location='Default.aspx'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
}
finally
{
con.Close();
}

标签: ASP.NET 备份 恢复 SqlServer 数据库


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持