ASP.NET 恢復(fù)備份Sqlserver實(shí)現(xiàn)代碼
前臺(tái)代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SqlDbMgmt.aspx.cs" Inherits="SysSourceMgmt.SqlDbMgmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 100px">
<span style="font-size: 9pt">操 作 數(shù) 據(jù) 庫(kù)</span>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Font-Size="9pt" Width="124px">
</asp:DropDownList>
<asp:TextBox ID="txtDbName" runat="server"></asp:TextBox>
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
<span style="font-size: 9pt">備份名稱和位置</span>
</td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server" Font-Size="9pt" Width="117px"></asp:TextBox>
</td>
<td style="width: 100px">
<span style="font-size: 9pt; color: #ff3300">(如D:\beifen)</span>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="備份數(shù)據(jù)庫(kù)" />
</td>
</tr>
</table>
</div>
<div style="width: 100%; height: 100px">
<table>
<tr>
<td style="width: 100px; height: 21px">
<span style="font-size: 9pt">操 作 數(shù) 據(jù) 庫(kù)</span>
</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" Font-Size="9pt" Width="124px">
</asp:DropDownList>
</td>
<td style="width: 100px; height: 21px">
</td>
</tr>
<tr>
<td style="width: 100px">
<span style="font-size: 9pt">操 作 數(shù) 據(jù) 庫(kù)</span>
</td>
<td style="width: 100px">
<asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" Width="190px" />
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td colspan="3">
<asp:Button ID="Button2" runat="server" Font-Size="9pt" OnClick="Button2_Click" Text="還原數(shù)據(jù)庫(kù)" />
<asp:Button ID="Button3" runat="server" Font-Size="9pt" OnClick="Button3_Click" Text="強(qiáng)制還原數(shù)據(jù)庫(kù)" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后臺(tái):
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=javascript>alert('此文件已存在,請(qǐng)從新輸入!');location='Default.aspx'</script>");
return;
}
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('備份數(shù)據(jù)成功!');'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('備份數(shù)據(jù)失??!')</script>");
}
finally
{
con.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string path = this.FileUpload1.PostedFile.FileName; //獲得備份路徑及數(shù)據(jù)庫(kù)名稱
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('還原數(shù)據(jù)成功!');'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('還原數(shù)據(jù)失??!')</script>");
txtDbName.Text = SqlStr2;
}
finally
{
con.Close();
}
}
/// <summary>
/// 恢復(fù)數(shù)據(jù)庫(kù),可選擇是否可以強(qiáng)制還原(即在其他人在用的時(shí)候,依然可以還原)
/// </summary>
/// <param name="databasename">待還原的數(shù)據(jù)庫(kù)名稱</param>
/// <param name="databasefile">帶還原的備份文件的完全路徑</param>
/// <param name="errormessage">恢復(fù)數(shù)據(jù)庫(kù)失敗的信息</param>
/// <param name="forceRestore">是否強(qiáng)制還原(恢復(fù)),如果為TRUE,則exec killspid '數(shù)據(jù)庫(kù)名' 結(jié)束此數(shù)據(jù)庫(kù)的進(jìn)程,這樣才能還原數(shù)據(jù)庫(kù)</param>
/// <returns></returns>
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)//如果強(qiáng)制回復(fù)
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; //獲得備份路徑及數(shù)據(jù)庫(kù)名稱
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);
}
}
}
效果圖:
經(jīng)過(guò)試驗(yàn),大體完成了我需要的功能,具體優(yōu)化后期進(jìn)行中。
相關(guān)文章
Visual Studio 2017新版發(fā)布 更強(qiáng)大!
Visual Studio 2017新版發(fā)布 更強(qiáng)大!對(duì)Visual Studio 2017感興趣的小伙伴們可以參考一下2017-05-05
asp.net中引用同一個(gè)項(xiàng)目中的類庫(kù) 避免goToDefinition時(shí)不能到達(dá)真正的定義類
asp.net中引用同一個(gè)項(xiàng)目中的類庫(kù) 避免 goToDefinition時(shí)不能到達(dá)真正的定義類2011-10-10
.net使用jwt進(jìn)行身份認(rèn)證的流程記錄
這篇文章主要給大家介紹了關(guān)于.net使用jwt進(jìn)行身份認(rèn)證的相關(guān)資料,JWT是Auth0提出的通過(guò)對(duì)JSON進(jìn)行加密簽名來(lái)實(shí)現(xiàn)授權(quán)驗(yàn)證的方案,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
.NET全局靜態(tài)可訪問(wèn)IServiceProvider的過(guò)程詳解(支持Blazor)
為解決在靜態(tài)方法中訪問(wèn)依賴注入(DI)容器的問(wèn)題,提出了通過(guò)DependencyInjection.StaticAccessor包實(shí)現(xiàn)靜態(tài)訪問(wèn),這一方法特別適用于需要在靜態(tài)方法中獲取范圍內(nèi)(Scoped)服務(wù)的場(chǎng)景,感興趣的朋友跟隨小編一起看看吧2024-09-09
解決ASP.NET回傳后div滾動(dòng)條位置復(fù)位的問(wèn)題(利用隱藏控件原理)
這篇文章主要介紹了解決ASP.NET回傳后div滾動(dòng)條位置復(fù)位的問(wèn)題,中心思想是用一個(gè)隱藏控件保存當(dāng)前scorll值?;貍骰貋?lái)后根據(jù)scrollTop的值,然后在Page_Load中重新設(shè)置scrollTop2014-01-01
ASP.NET實(shí)現(xiàn)可以縮放和旋轉(zhuǎn)的圖片預(yù)覽頁(yè)效果
本文詳細(xì)介紹了如何在ASP.NET?WebForms中實(shí)現(xiàn)一個(gè)功能豐富的圖片預(yù)覽頁(yè)面,通過(guò)結(jié)合HTML、CSS和JavaScript,用戶可以方便地對(duì)圖片進(jìn)行放大、縮小以及旋轉(zhuǎn)操作,感興趣的朋友跟隨小編一起看看吧2024-08-08
asp.net updatepanel 導(dǎo)致JS不能加載,而無(wú)法使用的解決方法
asp.net updatepanel 局部刷新,導(dǎo)致JS不能加載,而無(wú)法使用,而且 updatepanel會(huì)刷兩次,郁悶的,解決方法如下2013-08-08

