IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置:首頁網(wǎng)絡(luò)編程.Net編程 → ASP.NET恢復(fù)備份Sql server

ASP.NET恢復(fù)備份Sql server

時(shí)間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)

  最近做的一個(gè)項(xiàng)目因?yàn)榉⻊?wù)器是在特殊機(jī)房上的,因?yàn)榘踩矫娴目紤],不能給我們開發(fā)者提供FTP服務(wù),所以每次更新版本都得自己跑一趟,而他的機(jī)房有很遠(yuǎn),所以我一直想能不能開發(fā)一個(gè)維護(hù)版本的系統(tǒng)呢,對數(shù)據(jù)庫和代碼進(jìn)行在線更新,就不用自己跑了,于是就有了下面的嘗試,在線恢復(fù)和備份sql server

  前臺代碼:

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SqlDbMgmt.aspx.cs" Inherits="SysSourceMgmt.SqlDbMgmt" %>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>">

  <html xmlns="<https://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ù) 庫</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ù)庫" />

  </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ù) 庫</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ù) 庫</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ù)庫" />

  <asp:Button ID="Button3" runat="server" Font-Size="9pt" OnClick="Button3_Click" Text="強(qiáng)制還原數(shù)據(jù)庫" />

  </td>

  </tr>

  </table>

  </div>

  </form>

  </body>

  </html>

#p#副標(biāo)題#e#

  后臺:

  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('此文件已存在,請從新輸入!');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ù)庫名稱

  string dbName = string.Empty;

  if (DropDownList1.Items.Count != 0)

  {

  dbName = DropDownList1.SelectedV

關(guān)鍵詞標(biāo)簽:ASP.NET恢復(fù)備份Sql s

相關(guān)閱讀

文章評論
發(fā)表評論

熱門文章 手把手教你用好LINQ to SQL手把手教你用好LINQ to SQL在.NET環(huán)境下為網(wǎng)站增加IP過濾功能在.NET環(huán)境下為網(wǎng)站增加IP過濾功能ASP.NET 如何避免頁面重新整理時(shí)重復(fù)送出ASP.NET 如何避免頁面重新整理時(shí)重復(fù)送出用Asp.net擴(kuò)展ExtJS用Asp.net擴(kuò)展ExtJS

相關(guān)下載

人氣排行 asp.net表單提交方法GET\POST在ASP.NET中如何判斷用戶IE瀏覽器的版本Asp.net中messagebox的實(shí)現(xiàn)方法Asp.net中的web.config配置在ASP.NET MVC中實(shí)現(xiàn)大文件異步上傳用Iformattable接口控制.Net中文本格式c#.Net經(jīng)典面試題目用Asp.net擴(kuò)展ExtJS