Use the Asp.net program to back up or restore sqlserver

Source: Internet
Author: User

The SQL statements provided by SQL Server are used for backup.
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.

. Aspx 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> No title page </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>

. CS 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 ();
}
}
}

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.