您的位置:首页 > 数据库

数据库的备份与还原操作

2013-03-19 18:50 302 查看
View Code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;  //引用命名空间
using System.IO;   //引用命名空间

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string cmdtxt1 = ConfigurationSettings.AppSettings["strCon"];
//定义查询所有的数据库的SQL语句
string cmdtxt2 = "Exec sp_helpdb";
//创建数据库连接对象
SqlConnection Con = new SqlConnection(cmdtxt1);
//打开数据库连接
Con.Open();
//创建命令对象
SqlCommand mycommand = new SqlCommand(cmdtxt2, Con);
//创建一个数据阅读器
SqlDataReader dr = mycommand.ExecuteReader();
//将从数据库中读取到的数据作为dropSqlName数据源
this.dropSqlName.DataSource = dr;
//指定文本内容
this.dropSqlName.DataTextField = "name";
//从数据库中绑定数据
this.dropSqlName.DataBind();
//关闭数据阅读器
dr.Close();
//关闭数据库连接
Con.Close();
}
if (this.RadioButtonList1.SelectedIndex == 1)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = true;
}
else
{
this.Panel2.Visible = true;
this.Panel1.Visible = false;
this.Panel3.Visible = true;
this.Panel4.Visible = false;
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//定义数据库连接字串
string cmdtxt1 = "Server=localhost;database='" + this.dropSqlName.SelectedValue + "';Uid=sa;Pwd=123";
//定义备数据库的T-SQL命令的字符串
string cmdtxt2 = "backup database " + this.dropSqlName.SelectedValue + " to disk='" + this.TextBox1.Text.Trim() + ".bak'";
//创建数据库连接对象
SqlConnection Con = new SqlConnection(cmdtxt1);
//打开数据库连接
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(cmdtxt2, Con);
//执行数据库操作
Com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('备份数据成功!');location='Default.aspx'</script>");
}
catch (Exception ms)
{
Response.Write(ms.Message);
Response.Write("<script language=javascript>alert('备份数据失败!')</script>");
}
finally
{
//关闭数据库连接
Con.Close();
}
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
//获得备份路径及数据库名称
string path = this.fileShow.PostedFile.FileName;
//获取文件的后缀名
string last = path.Substring(path.LastIndexOf(".") + 1);
string dbname = this.dropSqlName.SelectedValue;
//判断是不是数据库备份文件
if (last == "bak")
{
//定义数据库连接字符串
string cmdtxt1 = "Server=localhost;database='" + this.dropSqlName.SelectedValue + "';Uid=sa;Pwd=123";
//定义实现还原数据库操作的字符串
string cmdtxt2 = "use master restore database " + dbname + " from disk='" + path + "'";
//定义数据库连接对象
SqlConnection Con = new SqlConnection(cmdtxt1);
//打开数据库连接
Con.Open();
try
{
//创建命令对象
SqlCommand Com = new SqlCommand(cmdtxt2, Con);
//执行数据库命令
Com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('还原数据成功!');location='Default.aspx'</script>");
}
catch (Exception ms)
{
Response.Write(ms.Message);
Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
}
finally
{
//关闭数据库连接
Con.Close();
}
}
else
{
Response.Write("<script language=javascript>alert('必须是数据库文件!');</script>");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: