国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

ASP.NET 恢復備份Sqlserver實現(xiàn)代碼

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 22:43:15
文檔

ASP.NET 恢復備份Sqlserver實現(xiàn)代碼

ASP.NET 恢復備份Sqlserver實現(xiàn)代碼:最近做的一個項目因為服務器是在特殊機房上的,因為安全方面的考慮,不能給我們開發(fā)者提供FTP服務,所以每次更新版本都得自己跑一趟,而他的機房有很遠,所以我一直想能不能開發(fā)一個維護版本的系統(tǒng)呢,對數(shù)據(jù)庫和代碼進行在線更新,就不用自己跑了,于是就有
推薦度:
導讀ASP.NET 恢復備份Sqlserver實現(xiàn)代碼:最近做的一個項目因為服務器是在特殊機房上的,因為安全方面的考慮,不能給我們開發(fā)者提供FTP服務,所以每次更新版本都得自己跑一趟,而他的機房有很遠,所以我一直想能不能開發(fā)一個維護版本的系統(tǒng)呢,對數(shù)據(jù)庫和代碼進行在線更新,就不用自己跑了,于是就有

最近做的一個項目因為服務器是在特殊機房上的,因為安全方面的考慮,不能給我們開發(fā)者提供FTP服務,所以每次更新版本都得自己跑一趟,而他的機房有很遠,所以我一直想能不能開發(fā)一個維護版本的系統(tǒng)呢,對數(shù)據(jù)庫和代碼進行在線更新,就不用自己跑了,于是就有了下面的嘗試,在線恢復和備份SQL Server:

前臺代碼:
代碼如下:


<%@ 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ù) 庫</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="強制還原數(shù)據(jù)庫" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

后臺:
代碼如下:

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.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>
/// 恢復數(shù)據(jù)庫,可選擇是否可以強制還原(即在其他人在用的時候,依然可以還原)
/// </summary>
/// <param name="databasename">待還原的數(shù)據(jù)庫名稱</param>
/// <param name="databasefile">帶還原的備份文件的完全路徑</param>
/// <param name="errormessage">恢復數(shù)據(jù)庫失敗的信息</param>
/// <param name="forceRestore">是否強制還原(恢復),如果為TRUE,則exec killspid '數(shù)據(jù)庫名' 結束此數(shù)據(jù)庫的進程,這樣才能還原數(shù)據(jù)庫</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)//如果強制回復
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ù)庫名稱
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)過試驗,大體完成了我需要的功能,具體優(yōu)化后期進行中。

聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

ASP.NET 恢復備份Sqlserver實現(xiàn)代碼

ASP.NET 恢復備份Sqlserver實現(xiàn)代碼:最近做的一個項目因為服務器是在特殊機房上的,因為安全方面的考慮,不能給我們開發(fā)者提供FTP服務,所以每次更新版本都得自己跑一趟,而他的機房有很遠,所以我一直想能不能開發(fā)一個維護版本的系統(tǒng)呢,對數(shù)據(jù)庫和代碼進行在線更新,就不用自己跑了,于是就有
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 老色99久久九九精品尤物 | 国产欧美综合一区二区 | 国产成人精品综合久久久 | 亚洲视频在线观看 | 午夜黄色在线观看 | 国产色综合久久无码有码 | 福利资源在线 | 国产成人精品久久二区二区 | 欧美精品亚洲人成在线观看 | 一区二区三区成人 | 国产日韩免费 | 日韩在线电影 | 极品国产高颜值露脸在线 | 正在播放国产一区 | 国产亚洲欧美精品久久久 | 日韩区在线| 亚洲精品在线免费观看 | 国产伦精品一区二区三区免费下载 | 国产全黄一级毛片 | 多人伦精品一区二区三区视频 | 青青操国产视频 | 91午夜精品亚洲一区二区三区 | 亚洲 欧美 国产另类首页 | 波多野结衣免费在线 | 成人a免费视频播放 | 亚洲精品国产精品国自产观看 | 伊人久久影院 | 久久精品一级 | 国产九九精品 | 免费在线不卡视频 | 操日韩 | 国产中出 | 夜夜骑首页 | 国产va精品免费观看 | 国产在线精品一区二区三区 | 国产一区二区三区久久 | 在线观看国产 | 亚洲精品不卡久久久久久 | 欧美在线视频免费观看 | 亚洲国产成人精品女人久久久 | www.久久99|