ASP.NET中数据库备份的源码
using System;
using System.Data.SqlClient;
using System.Web.UI;
public partial class BackupDatabase : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["UserName"] == null && Session["Pwd"] == null)
{
Response.Redirect("Prompt.aspx");
}
else
{
LblMessage.Text = "您好! " + Session["UserName"] + " 欢迎您登录本网站!";
try
{
BindDropDownList();
}
catch
{
BindDropDownList();
}
}
}
}
public void BindDropDownList()
{
string cmdtxt1 = "Data Source=localhost;Initial Catalog=master;Integrated Security=True";
string cmdtxt2 = "Exec sp_helpdb";
SqlConnection connection = new SqlConnection(cmdtxt1);
connection.Open();
SqlCommand command = new SqlCommand(cmdtxt2, connection);
SqlDataReader dr = command.ExecuteReader();
DDLDataBase.DataSource = dr;
DDLDataBase.DataTextField = "name";
DDLDataBase.DataBind();
dr.Close();
connection.Close();
}
protected void ImgBtnOK_Click(object sender, ImageClickEventArgs e)
{
string cmdtxt1 = "Data Source=localhost;Initial Catalog=‘" + DDLDataBase.SelectedValue +
"‘;Integrated Security=True";
string cmdtxt2 = "backup database " + DDLDataBase.SelectedValue + " to disk=‘" + TextBox1.Text.Trim() + ".bak‘";
SqlConnection connection = new SqlConnection(cmdtxt1);
connection.Open();
try
{
SqlCommand command = new SqlCommand(cmdtxt2, connection);
command.ExecuteNonQuery();
Response.Write("<script language=javascript>alert(‘备份数据成功!‘);location=‘javascript:history.go(-1)‘</script>");
}
catch (Exception ms)
{
Response.Write(ms.Message);
Response.Write("<script language=javascript>alert(‘备份数据失败!‘);location=‘javascript:history.go(-1)‘</script>");
}
finally
{
connection.Close();
}
}
本文出自 “黄忠” 博客,请务必保留此出处http://tyjhz.blog.51cto.com/8756882/1395565
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。