首页 > 编程学习 > Asp.net(C#)中备份还原SQL Server数据库实现

在web中,安全性主要体现在两个方面:一个是程序安全性,即防止网页在插入恶意代码;另一个是数据库安全性,这个我们可以经常备份数据库来实现。
在文中,我将演示如果在网页中备份和恢复数据库。
其实备份和恢复数据库都是利用SQL Server提供的SQL语句来备份的。
备份:use master;backup database @name to disk=@path;
恢复:use master;restore database @name from disk=@path;
上面用的是参数化SQL语句,可以在程序执行的时候动态给参数赋值。

 

代码:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DatabaseAction.aspx.cs" Inherits="DatabaseAction" %>

<!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>網絡大本營(From:www.QQView.com)</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="0" width="100%">
    <tr><td colspan="2">数据库还原和备份</td></tr>
    <tr><td>请选择数据库</td><td>
        <asp:DropDownList ID="ddlDatabaseList" runat="server">
        </asp:DropDownList></td></tr>
    <tr><td>
        数据库文件名</td><td>
        <asp:TextBox ID="txtDbFileName" runat="server"></asp:TextBox></td></tr>
    <tr><td>
        操作选项</td><td>
        <asp:RadioButton ID="rbBackup" runat="server" Checked="True" GroupName="action" Text="备份" />
        <asp:RadioButton ID="rbRestore" runat="server" GroupName="action" Text="还原" /></td></tr>
        <tr><td>
        操作</td><td>
             <asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="执行" /></td></tr>
    </table>
    </div>
    </form>
</body>
</html>
 

后台代码:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// 功能说明:本例中演示在asp.net中如何备份和恢复数据库
/// 备份数据库主要使用数据库的备份语句。数据库备份文件放在
/// App_Data文件夹下。
/// 作者:周公
/// 日期:2008-08-19
/// 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2008/08/19/2796077.aspx
/// </summary>
public partial class DatabaseAction : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //在DropDownList中绑定所有数据库
            SqlConnection connection = new SqlConnection("Data Source=ZHOUFOXCN;User ID=sa;Password=sa");
            SqlCommand command = new SqlCommand("sp_helpdb", connection);
            command.CommandType = CommandType.StoredProcedure;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            ddlDatabaseList.DataSource = reader;
            ddlDatabaseList.DataTextField = "Name";
            ddlDatabaseList.DataBind();
            reader.Close();
            connection.Close();
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string dbFileName = txtDbFileName.Text.Trim();
        SqlConnection connection = new SqlConnection("Data Source=ZHOUFOXCN;User ID=sa;Password=sa");
        string dbName = ddlDatabaseList.SelectedValue;
        if (!dbFileName.EndsWith(".bak"))
        {
            dbFileName += ".bak";
        }
        if (rbBackup.Checked)//备份数据库
        {
            SqlCommand command = new SqlCommand("use master;backup database @name to disk=@path;",connection);
            connection.Open();
            string path=Server.MapPath("~\\App_Data")+"\\"+dbfilename;
            command.Parameters.AddWithValue("@name", dbName);
            command.Parameters.AddWithValue("@path", path);
            command.ExecuteNonQuery();
            connection.Close();
        }
        else//恢复数据库
        {
            SqlCommand command = new SqlCommand("use master;restore database @name from disk=@path;", connection);
            connection.Open();
            string path = Server.MapPath("~\\App_Data") + "\\" + dbFileName;
            command.Parameters.AddWithValue("@name", dbName);
            command.Parameters.AddWithValue("@path", path);
            command.ExecuteNonQuery();
            connection.Close();
        }
    }
}
以上代码在WindowsXP+VisualStudio2005+SQL Server2000下测试通过


该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/200811520559.Html

转载于:https://www.cnblogs.com/Jackey_Chen/archive/2008/12/12/1353712.html

Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000