How can I back up a database on the server to a local hard disk through web?

Source: Internet
Author: User
Tags ftp protocol
Problem description:

How can I back up a database on the server to a local hard disk through web?

Solution:


1. Make sure that the web site and database are deployed on the same server.

Database Backup can only be backed up to a local hard disk. If the database and the Web server are not on the same server, it is troublesome to download the database backup on the database server through the web site, we will introduce how to solve the problem that is not on the same server.


2. Use sqldmo to back up the database to a specified location on the server.

We can use sqldmo to back up databases. To access sqldmo, we must add references to the sqldmo COM component and add references. In the displayed dialog box, select the com tab, find the component: Microsoft sqldmo Object Library, click OK, add the component to our web site, and back up the database using the following code:

Here, strfilename is the location where the database backup is saved (the location on the server). To simplify the download implementation, we set strfilename to the virtual directory of the web project, we can set it to the root directory, then strfilename = server. mappath ("/DB. bak ");

 

1 Public bool backupdb (string servername, string username, string password, string strdbname, string strfilename)
2 {
3
4 sqldmo. svr SVR = new sqldmo. sqlserverclass ();
5
6 try
7
8 {
9
10 SVR. Connect (servername, username, password );
11
12 sqldmo. Backup Bak = new sqldmo. backupclass ();
13
14 Bak. Action = 0;
15
16 Bak. initialize = true;
17
18 Bak. Files = strfilename;
19
20 Bak. Database = strdbname;
21
22 Bak. sqlbackup (SVR );
23
24 return true;
25
26}
27
28 catch (exception ERR)
29
30 {
31
32 throw new exception ("failed to back up the database:" + err. Message );
33
34}
35
36 finally
37
38 {
39
40 SVR. Disconnect ();
41
42}
43
44}
45

 


3. Download Database Backup

According to 2. in the database backup file, we know that the backup file to be downloaded is: file = server. mappath ("/DB. bak "); then we can download the database backup by calling download (file.

 

1 Public void download (string file)
2
3 {
4
5 fileinfo Fi = new fileinfo (File );
6
7 response. addheader ("Content-Length", Fi. length. tostring ());
8
9 response. contenttype = "application/octet-stream ";
10
11 response. addheader ("content-disposition", String. Format ("attachment; filename = {0}", httputility. urlencode (file, encoding. utf8 )));
12
13
14
15 response. writefile (File );
16
17}
18

 

Note: to allow users to download backups, we must back up the database to the IIS Directory of the web project.


4. Deficiencies and improvements in the solution.

The above two steps are used to back up and save the database to a local hard disk through web, provided that the Web and database must be deployed on the same server.

So how can we solve the problem if the web site and database are deployed on different machines? In fact, this is also the case. You only need to ensure that the storage location of the backup files in our database is transparent to the web project and can be accessed through the web.

For example, you can create a virtual directory on the database server to point to the database backup location, and then access the backup file based on the virtual directory on the web. Alternatively, you can create an FTP directory pointing to the database backup location, and download the backup file through the FTP protocol on the web. You can also use the remoting service.

Original article: http://www.cnblogs.com/linlimin/articles/shangli-xiamen-fujian.html

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.