
时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)
最近做的一个项目因为服务器是在特殊机房上的,因为安全方面的考虑,不能给我们开发者提供FTP服务,所以每次更新版本都得自己跑一趟,而他的机房有很远,所以我一直想能不能开发一个维护版本的系统呢,对数据库和代码进行在线更新,就不用自己跑了,于是就有了下面的尝试,在线恢复和备份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&>操 作 数 据 库</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=&备份数据库& />
</td>
</tr>
</table>
</div>
<div style=&width: 100%; height: 100px&>
<table>
<tr>
<td style=&width: 100px; height: 21px&>
<span style=&font-size: 9pt&>操 作 数 据 库</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&>操 作 数 据 库</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=&还原数据库& />
<asp:Button ID=&Button3& runat=&server& Font-Size=&9pt& OnClick=&Button3_Click& Text=&强制还原数据库& />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
#p#副标题#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('备份数据成功!');'</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.SelectedV
关键词标签:ASP.NET恢复备份Sql s
相关阅读 ASP.NET创建XML Web服务全接触 如何提高ASP.NET页面载入速度的方法 .net导出海量数据到execl文件 ASP.NET 2.0程序安全的基础知识 ASP.NET中MD5与SHA1加密的几种方法 在.net开发中几个重要的认识误区
热门文章
在ASP.NET MVC中实现大文件异步上传
在.NET环境下为网站增加IP过滤功能
诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022
ASP.NET 如何避免页面重新整理时重复送出
钟离圣遗物推荐-原神钟离圣遗物词条
人气排行 诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022 asp.net表单提交方法GETPOST 在ASP.NET中如何判断用户IE浏览器的版本 Asp.net中messagebox的实现方法 Asp.net中的web.config配置 在ASP.NET MVC中实现大文件异步上传 asp.net获取URL和IP地址 FileUpload上传多文件出现错误的解决方法 ASP.NET Web.config配置详解 Asp.net常用的51个代码(非常实用) ASP.NET打开word文档出错的解决办法 让你的.NET程序兼容不同版本的Dll文件
查看所有0条评论>>