Client backup and restoration database (ASP. NET)

Source: Internet
Author: User

Database backup and restoration are the features required by many customers in project development. They hope that the backup database can be downloaded to the local storage when the backup button is clicked. Click the restore button to restore the local database to the server.

Although this is not safe, it is also necessary in the LAN.

Several knowledge points are used here.

1: database backup, and restoration (SQL Server)

2: upload and download files. (Why do I need to upload and download files? There is a very noteworthy problem here, and I often neglect it when working on projects. We performed database backup and restoration locally. That is to say, our computer is both a client and a server. So no matter how you test it, as long as the code is not wrong, it will succeed. Once published on the server, it will appear again during debugging. The database is backed up on the server, and the restoration of the database will also fail. Therefore, you need to download the database to the local storage, upload the data to the server during restoration, and use SQL to read the path on the server for restoration .)

 

Below is my code. I encapsulate backup and restoration in a class.

Public class outdata

{

  

/// <Summary>
/// Back up the database

/// Xianningdsh is the name of the database I tested
/// </Summary>
Public static int backdata (string name)
{

String sqlstr = "use xianningdsh; backup database xianningdsh to disk = '" + name + "'";
Return executesql (sqlstr );

}
/// <Summary>
/// Restore the database
/// </Summary>
Public static int rebackdata (string name)
{

String sqlstr = "use master; Restore database xianningdsh from disk = '" + name + "'";
Return executesql (sqlstr );
}

}

At the front-end:

 

Protected void button#click (Object sender, eventargs E)
{
// Back up the database

// Randomly generate the backup database name
String filename = httputility. urldecode (request. Cookies ["username"]. Value) + datetime. Now. ticks. tostring () + "database backup. Bak ";

Try
{
Outdata (server. mappath ("..") + "// uploadfile //" + filename );
Addnote (filename); // Save the full path of the backup data to the XML file.
JScript. alertandredirect ("Data Backup successful! "," Importdata. aspx ");

}
Catch (exception me)
{
JScript. Alert (Me. Message );
}
}
Protected void button2_click (Object sender, eventargs E)
{
// Restore the database

String dric = server. mappath ("..") + "// uploadfile //" + fileupload1.filename;
Fileupload1.saveas (dric); // upload and restore the database to the server
Try
{
Outdata (dric );
JScript. alertandredirect ("database restored successfully! "," Importdata. aspx ");
}
Catch
{
JScript. alertandredirect ("database restoration failed! "," Importdata. aspx ");
}
}

 

When processing database files on the download server, it is not output to the local device as a file stream. Instead, the saved database file path is saved to the XML file (dataconfig. XML ).

<? XML version = "1.0" encoding = "UTF-8"?>
<Dataconfig>
<DATA>
<Time> Backup time </time>
<Uname> Backup recipient </uname>
<URL> download </URL>
</Data>
<DATA>
<Time> 14:43:34 </time>
<Uname> admin </uname>
<URL> admin633930290128125000 backup. Bak </URL>
</Data>
<DATA>
<Time> 14:46:59 </time>
<Uname> admin </uname>
<URL> admin633930292183593750 backup. Bak </URL>
</Data>
</Dataconfig>

Therefore, processing of XML files is also needed here. In the next blog, I will introduce the addition, deletion, modification, and query functions for XML files.

Note that when using VSS to manage our projects, you must set the XML file attribute to read/write.

Below is my.

 


 

 

 

 

 

 

 

 

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.