How to back up and restore an SQL Server database in Asp.net (C #)

Source: Internet
Author: User

In the web, security is mainly reflected in two aspects: one is program security, that is, preventing malicious code from being inserted into the web page; the other is database security, which we can often back up the database for implementation.
In this article, I will demonstrate how to back up and restore databases on the web page.
In fact, both backup and restoration of databases are backed up using SQL statements provided by SQL Server.
Backup: use master; backup database @ name to disk = @ path;
Recovery: use master; restore database @ name from disk = @ path;
The preceding parameterized SQL statement can be used to dynamically assign values to parameters during program execution.

 

Code:

<% @ 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> webpage (From: www.QQView.com) </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Table border = "0" width = "100%">
<Tr> <td colspan = "2"> database restoration and backup </td> </tr>
<Tr> <td> select a database </td> <td>
<Asp: DropDownList ID = "ddlDatabaseList" runat = "server">
</Asp: DropDownList> </td> </tr>
<Tr> <td>
Database file name </td> <td>
<Asp: TextBox ID = "txtDbFileName" runat = "server"> </asp: TextBox> </td> </tr>
<Tr> <td>
Operation options </td> <td>
<Asp: RadioButton ID = "rbBackup" runat = "server" Checked = "True" GroupName = "action" Text = "backup"/>
<Asp: RadioButton ID = "rbRestore" runat = "server" GroupName = "action" Text = "Restore"/> </td> </tr>
<Tr> <td>
Operation </td> <td>
<Asp: Button ID = "btnOK" runat = "server" OnClick = "btnOK_Click" Text = ""/> </td> </tr>
</Table>
</Div>
</Form>
</Body>
</Html>
 

Background code:

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>
/// Function Description: This example shows how to back up and restore a database in asp.net.
/// Backup the database mainly uses the backup statement of the database. Store the database backup file
/// Under the App_Data folder.
/// By Zhou Gong
/// Date: 2008-08-19
/// Starting address: 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)
{
// Bind all databases to the 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) // back up the database
{
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 // restore the database
{
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 ();
}
}
}
The above code is tested and passed in WindowsXP + VisualStudio2005 + SQL Server2000

This article Reprinted from the network base camp: http://www.xrss.cn/Dev/DotNet/200811520559.Html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.