ASP.NET 还原数据库与 备份代码

这是一款超漂亮的ASP.NET 还原数据库教程与 备份代码哦,好了下面我们以实例来讲一下关于.net教程还原与备份数据库代码吧。

html代码
<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></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>

asp.net 核心代码

using System.Data.SqlClient;
using System.IO;
string SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
string SqlStr2 = "Exec sp_helpdb";
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "backup database " + this.DropDownList1.SelectedValue + " to disk='" + this.TextBox1.Text.Trim() + ".bak'";

功能页面

using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "backup database " + this.DropDownList1.SelectedValue + " 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('备份数据成功!');location='Default.aspx'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('备份数据失败!')</script>");
}
finally
{
con.Close();
}
}
}

还原数据库

string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";

html代码

<table>
<tr>
<td style="width: 100px; height: 21px"><span style="font-size: 9pt">操 作 数 据 库</span></td>
<td><asp:DropDownList ID="DropDownList1" 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="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="还原数据库" /></td>
</tr>
</table>

还原数据库功能页面

using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
string dbname = this.DropDownList1.SelectedValue;
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
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('还原数据成功!');location='Default.aspx'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
}
finally
{
con.Close();
}
}
}

时间: 2024-10-31 16:31:52

ASP.NET 还原数据库与 备份代码的相关文章

asp控制xml数据库的经典代码_应用技巧

NO.1--建立一个XML数据库data.xml <?xml version="1.0"?> <records> <record> <name>caca</name> <qq>154222225</qq> <email>root@3ney.com</email> </record> <records> NO.2--建立对象CreateObject 建立da

asp控制xml数据库的经典代码

NO.1--建立一个XML数据库data.xml <?xml version="1.0"?> <records> <record> <name>caca</name> <qq>154222225</qq> <email>root@3ney.com</email> </record> <records> NO.2--建立对象CreateObject 建立da

asp连接mysql数据库几款代码

asp教程连接mysql教程数据库教程几款代码 strconnection  =  "dsn=hc188;driver={myodbd  driver};server=localhost;uid=root;pwd=;database=hc188"    set  adodataconn  =  server.createobject("adodb.connection")    adodataconn.open  strconnection  方法二 strconn

asp连接mysql数据库详细实现代码

想要asp能连接mysql数据库需要安装MySQL ODBC 3.51 驱动 http://www.jb51.net/softs/19910.html 我们先看下面这段代码 复制代码 代码如下: set conn = server.createobject("adodb.connection") Conn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=127.0.0.1;DATABASE=Shops;USER=root;PASSWO

asp连接mysql数据库详细实现代码_应用技巧

想要asp能连接mysql数据库需要安装MySQL ODBC 3.51 驱动 http://www.jb51.net/softs/19910.html我们先看下面这段代码 复制代码 代码如下: set conn = server.createobject("adodb.connection") Conn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=127.0.0.1;DATABASE=Shops;USER=root;PASSWOR

asp实现读取数据库输出json代码

代码如下: Function GetTable(Table,Where,Order,OrderSort,curpage, pagesize,MiniJson) 'Author : nigou '使用方法 : Response.Write GetTable(Table表名,Where条件,Order主键(必须),OrderSort(asc,desc),curpage当前页, pagesize每页条数,MiniJson是否输出为miniUI格式) ' dim i, j ,rs if OrderSor

asp实现读取数据库输出json代码_应用技巧

复制代码 代码如下: Function GetTable(Table,Where,Order,OrderSort,curpage, pagesize,MiniJson) 'Author : nigou '使用方法 : Response.Write GetTable(Table表名,Where条件,Order主键(必须),OrderSort(asc,desc),curpage当前页, pagesize每页条数,MiniJson是否输出为miniUI格式) '     dim i, j ,rs   

MySQL-Front数据库的备份与还原步骤

1.备份数据库 MySQL数据库建立后,我们需要经常备份数据库,以确保数据的在遭受损失后能最大程度的恢复.备份数据库的方法有很多,现在还有一些专门备份MySQL数据的工具,如帝国备份王.但是最常用的备份方式还是将MySQL订单数据库导出为SQL文件,后缀为".sql",SQL文件是SQL脚本文件. 在MySQL-Front中,可以将整个数据库导出为一个SQL文件或其它类型文件,也可以单独的将一个表导出为SQL文件或其它类型文件,导出的方法是:在需要导出的数据库或表上右键→导出→选择导出

mysql MySqlDump数据库备份代码

mysql教程 MySqlDump数据库教程备份代码 DELIMITER $$ CREATE DEFINER=`root`@`localhost` FUNCTION `UF_Get_tMaxCode`(   V_iType INT ) RETURNS varchar(10) CHARSET utf8 BEGIN /* 业务描述:根据编码类别产生编码 输入参数: V_iType:编码类别,定义建tMaxCode的iType字段的定义 调用示例: SELECT UF_Get_tMaxCode(1);