Asp.net(C#)中备份还原SQL Server数据库实现

zz/2023/6/3 15:16:29

在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

http://www.ngui.cc/zz/483567.html

相关文章

silverlight寻奇 - Graphite

Graphite是一个能自动布局的图表控件。 目前它已经有了silverlight 2 和 wpf的版本。观看demo时按下“Ctrl”键再做点击操作。 原文地址&#xff1a;http://www.orbifold.net/default/?page_id1270 Features common to both versions Styling: nodes can be styled or templ…

NTLDR is missing 的解决方法

NTLDR is missing 的解决方法 NTLDR文件是win nt/win200/WinXP的引导文件&#xff0c;当此文件丢失时启动系统会提示"NTLDR is missing..."并要求按任意键重新启动&#xff0c;不能正确进入系统 。所以应该在系统正常的时候给予备份。 NTLDR文件是做什么的…

iframe 跨域 cookies操作

192.168.14.69:http://192.168.14.69/payweb/iframe/default.aspx 客户端页面 通过js 动态输出iframe到localhost &#xff1a;default.aspx js Codevar linkswindow.document.getElementsByTagName(link);var link;for(var i0;i<links.length;i){ if(links[i].rel.toLow…

C# .net视频

赤壁下在线观看编程视频教程 赤壁下在线观看 http://www.5hht.com/bbs/Topic.aspx?BoardID52&TopicID2429 给各位编程的朋友好点的视频教程 全部微软官方讲师讲解,同时提供视频教程的源码下载,给我学习编程非一般的进步 .NET 编程系列课程(总共41课) http://www.5hht.com…

【转】jQuery 1.3 正式版 下载

jQuery 1.3 正式版发布 来源:www.zhangjingwei.comjQuery 1.3 经过几个Beta测试以后&#xff0c;今天正式发布了。较之1.2.6,它做要有这样一些更新。 英文文档&#xff1a;http://docs.jquery.com/Release:jQuery_1.3一、更为强大的选择器。jQuery 1.3 的选择器较之以前的更为…

多媒体交互应用基础(11)

问题一 如何使用键盘进行交互使用&#xff1f; 步骤&#xff1a; 1.我们首先找到管理键盘类&#xff0c;了解这个类能够为我们带来什么帮助&#xff0c;通过帮助能够解决什么问题。 2.导入需要使用键盘的类 3.监听键盘输入了解键盘事件 package { import flash.display.Sprite;…

Windows Live Writer 测试

test Test 转载于:https://www.cnblogs.com/suhongwei/archive/2009/02/03/1382897.html

C#代码调用js函数,js函数中的document.getElementById(对象ID)得null值解决办法

不知道大家有没有遇到这种情况&#xff0c;反正我就遇到过&#xff0c;我现在把解决方法写在下面。 解决方法:1,把调用js函数些在</from>下面。&#xff08;我原来是写在head标签里面&#xff09; 2,在cs页面定义一个全局变量&#xff0c;注意&#xff01;&#xff01;&a…

使用datareader检索数据

CodeSqlDataReader reader command.ExecuteReader();以下代码示例循环访问一个 DataReader 对象&#xff0c;并从每个行中返回两个列。 Codeif (reader.HasRows) while (reader.Read()) Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0), reader.GetStrin…

J2me联网线程

Code/** * Runnable类必须实现的方法 */ public void run() { String connStr preURLStr urlArgs; if (wapConnFlag) { boolean getDataFlag false; HttpConnection hcon null; DataInputStream dis n…