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