前台代码:
C#" AutoEventWireup="true" CodeBehind="SqlDbMgmt.x.cs" Inherits="SysSourceMgmt.SqlDbMgmt" %>
操 作 数 据 库 备份名称和位置 (如D:beifen) 操 作 数 据 库 操 作 数 据 库 后台: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.IO; using System.Data; using System.Diagnostics; namespace SysSourceMgmt { public partial class SqlDbMgmt : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { string SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd="; string SqlStr2 = "Exec sp_helpdb"; SqlConnection con = new SqlConnection(SqlStr1); con.Open(); SqlCommand com = new SqlCommand(SqlStr2, con); SqlDataReader dr = com.ExecuteReader(); this.DropDownList1.DataSource = dr; this.DropDownList1.DataTextField = "name"; this.DropDownList1.DataBind(); dr.Close(); con.Close(); SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd="; SqlStr2 = "Exec sp_helpdb"; con = new SqlConnection(SqlStr1); con.Open(); com = new SqlCommand(SqlStr2, con); dr = com.ExecuteReader(); this.DropDownList1.DataSource = dr; this.DropDownList1.DataTextField = "name"; this.DropDownList1.DataBind(); dr.Close(); con.Close(); } catch (Exception) { } } } protected void Button1_Click(object sender, EventArgs e) { string dbName = string.Empty; if (DropDownList1.Items.Count != 0) { dbName = DropDownList1.SelectedValue.Trim(); } else { dbName = txtDbName.Text.Trim(); } string SqlStr1 = "Data Source=.\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True"; string SqlStr2 = "backup database " + dbName + " 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="">alert('此文件已存在,请从新输入!');location='Default.aspx'</script>"); return; } SqlCommand com = new SqlCommand(SqlStr2, con); com.ExecuteNonQuery(); Response.Write("<script language="javascript">alert('备份数据成功!');'</script>"); } catch (Exception error) { Response.Write(error.Message); Response.Write("<script language="javascript">alert('备份数据失败!')</script>"); } finally { con.Close(); } } protected void Button2_Click(object sender, EventArgs e) { string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称 string dbName = string.Empty; if (DropDownList1.Items.Count != 0) { dbName = DropDownList1.SelectedValue.Trim(); } else { dbName = txtDbName.Text.Trim(); } string SqlStr1 = "Data Source=.\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True"; 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('还原数据成功!');'</script>"); } catch (Exception error) { Response.Write(error.Message); Response.Write("<script language="javascript">alert('还原数据失败!')</script>"); txtDbName.Text = SqlStr2; } finally { con.Close(); } } /// /// 恢复数据库,可选择是否可以强制还原(即在其他人在用的时候,依然可以还原) /// /// 待还原的数据库名称 /// 带还原的备份文件的完全路径 /// 恢复数据库失败的信息 /// 是否强制还原(恢复),如果为TRUE,则exec killspid '数据库名' 结束此数据库的进程,这样才能还原数据库 /// public bool RestoreDataBase(string databasename, string databasefile, ref string returnMessage, bool forceRestore, SqlConnection conn) { bool success = true; string path = databasefile; string dbname = databasename; string restoreSql = "use master;"; if (forceRestore)//如果强制回复 restoreSql += string.Format("use master exec killspid '{0}';", databasename); restoreSql += "restore database @dbname from disk = @path;"; SqlCommand myCommand = new SqlCommand(restoreSql, conn); myCommand.Parameters.Add("@dbname", SqlDbType.Char); myCommand.Parameters["@dbname"].Value = dbname; myCommand.Parameters.Add("@path", SqlDbType.Char); myCommand.Parameters["@path"].Value = path; Response.Write(restoreSql); try { myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); returnMessage = "还原成功"; } catch (Exception ex) { returnMessage = ex.Message; success = false; } finally { myCommand.Connection.Close(); } return success; } protected void Button3_Click(object sender, EventArgs e) { string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称 string dbName = string.Empty; if (DropDownList1.Items.Count != 0) { dbName = DropDownList1.SelectedValue.Trim(); } else { dbName = txtDbName.Text.Trim(); } string returnMessage = string.Empty; string SqlStr1 = "Data Source=.\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True"; SqlConnection con = new SqlConnection(SqlStr1); RestoreDataBase(txtDbName.Text, path, ref returnMessage, true,con); Response.Write(returnMessage); } } } 2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英 2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce Win11 21H2版本停止服务要升级系统吗? SQLServer执行计划成本(续1) 修改电脑、伺服器名称后,SQL Server 的修正作业--补遗篇 新手必备,教你轻松安装Win10系统! Windows开机黑屏如何重装系统?【图文详解】 云计算之路-阿里云上:超级奇怪的“黑色10秒钟” 解决SYS本地登录或远程登录引起ORA-01031错误方法 ora-12154:tns:无法解析指定的连接标识符 创建 Oracle 分页存储过程 NoSQL学习之路(四):创建、读
编辑推荐:
相关推荐
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
热文推荐
