ASP. NET backup and restoration of SQL Server databases

Source: Internet
Author: User
ASP. NET backup and restoration of SQL Server databases

1. Backup

SqlConnection connection = new SqlConnection (ConfigurationManager. etettings ["Connection"]);
String dbFileName = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". bak ";
Try
{
SqlCommand command = new SqlCommand ("use master; backup database xxdl to disk = @ path;", connection );
Connection. Open ();
String path = Server. MapPath ("~ \ App_Data ") +" \ "+ dbFileName;
Command. Parameters. AddWithValue ("@ path", path );
Command. ExecuteNonQuery ();
LblBackup. Text = "Database Backup succeeded ";
}
Catch (Exception ex)
{

LblBackup. Text = ex. Message;
}
Finally
{
Connection. Close ();
}

Bind (); // a new backup file is generated and the update drop-down box is displayed.

2. Restore

SqlConnection conn = new SqlConnection ("Server =. \ sqlexpress; database = master; uid = sa; pwd = sa ");
String dbFileName = ddlRestore. SelectedValue;
If (dbFileName = string. Empty)
{
LblRestore. Text = "no database backup file, please back up first ";
Return;
}
Try
{
String sql2 = "Alter Database xxdl Set Offline with Rollback immediate; use master; restore database xxdl from disk = @ path With Replace; Alter Database xxdl Set OnLine With rollback Immediate ;";
SqlCommand command = new SqlCommand (sql2, conn );
Conn. Open ();
String path = Server. MapPath ("~ \ App_Data ") +" \ "+ dbFileName;
Command. Parameters. AddWithValue ("@ path", path );
Command. ExecuteNonQuery ();
LblRestore. Text = "database restored successfully ";
Session. Abandon ();
Response. Redirect ("login. aspx ");
}
Catch (System. Exception ex)
{
LblRestore. Text = ex. Message;
}
Finally
{
Conn. Close ();
}

1. Backup

SqlConnection connection = new SqlConnection (ConfigurationManager. etettings ["Connection"]);
String dbFileName = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". bak ";
Try
{
SqlCommand command = new SqlCommand ("use master; backup database xxdl to disk = @ path;", connection );
Connection. Open ();
String path = Server. MapPath ("~ \ App_Data ") +" \ "+ dbFileName;
Command. Parameters. AddWithValue ("@ path", path );
Command. ExecuteNonQuery ();
LblBackup. Text = "Database Backup succeeded ";
}
Catch (Exception ex)
{

LblBackup. Text = ex. Message;
}
Finally
{
Connection. Close ();
}

Bind (); // a new backup file is generated and the update drop-down box is displayed.

2. Restore

SqlConnection conn = new SqlConnection ("Server =. \ sqlexpress; database = master; uid = sa; pwd = sa ");
String dbFileName = ddlRestore. SelectedValue;
If (dbFileName = string. Empty)
{
LblRestore. Text = "no database backup file, please back up first ";
Return;
}
Try
{
String sql2 = "Alter Database xxdl Set Offline with Rollback immediate; use master; restore database xxdl from disk = @ path With Replace; Alter Database xxdl Set OnLine With rollback Immediate ;";
SqlCommand command = new SqlCommand (sql2, conn );
Conn. Open ();
String path = Server. MapPath ("~ \ App_Data ") +" \ "+ dbFileName;
Command. Parameters. AddWithValue ("@ path", path );
Command. ExecuteNonQuery ();
LblRestore. Text = "database restored successfully ";
Session. Abandon ();
Response. Redirect ("login. aspx ");
}
Catch (System. Exception ex)
{
LblRestore. Text = ex. Message;
}
Finally
{
Conn. 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.