asp.net online backup, compression, and Repair Access database sample code

Source: Internet
Author: User
Tags access database backup
This article mainly describes how to asp.net online backup, compression and repair of Access database, the need for friends can refer to the next 1. Question     When designing small and medium Web applications, you can choose Microsoft Accesss as a database. Frequent additions and deletions are made during the use of the database. In fact, Microsoft Access does not effectively release allocated but deleted object space, which means that even if you delete an object, the object still occupies the space of the database, making the database bigger and larger. Not only occupy unnecessary space, but also reduce the efficiency of the database. Especially in the virtual site of the problem is particularly prominent. Therefore, compressing the Access database is of practical significance.   Although the Access database itself has the "Compress and Repair Database" feature (tool è database utility è compression and repair database). However, it is inconvenient for the general user to operate. Usually the ACCESSS database is placed on the virtual host, need to download it down "compression repair" and then pass up a waste of time, so it is best to compress the database online.     2. The implementation of online compression database     2.1. Add reference to     Add a reference under the Vs.net environment solution. The methods are as follows: Items → add references → tabs → browse (C:/Program files/comm files/system/ado/msjro.dll).     2.2. Create a Web application form     Place a button on a Web Form (database.aspx):  code as follows: <asp:button id= "COMPACTBTN" runat= "Server" text= "Compress Database" onclick= "Compactbtn_click"/>  Add a Label control:  <asp:label id= "Msglabel" runat= "Server" ></asp:Label>    2.3. In code state, add a reference   code as follows: using system;  using System.IO;   using jro;    2.4. Add code     code as follows://Compressed database   protected VOID Compactbtn_click (object sender, EventArgs e)   {  string DbPath1, DbPath2, DbConn1, dbconn2;    DBPA Th1 = Server.MapPath (".. /app_data/database.mdb ")//Original database path   DBPATH2 = Server.MapPath (". /app_data/database2.mdb ")//Compressed database path   DBCONN1 =" Provider=Microsoft.Jet.OLEDB.4.0;Data source= "+ DbPath1;   DBCONN2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + dbpath2;    try  {  jetengine Data Baseengin = new JetEngine ();  databaseengin.compactdatabase (DbConn1, DBCONN2);//compression     File.Copy ( DbPath2, DbPath1, true);//The compressed database is overwritten with the original database   File.delete (DBPATH2);//Delete compressed database     Msglabel.text = "database compression succeeded !";  }  catch  {  msglabel.text = "Database compression failed, please try again!";  } }    3. Backup DATABASE     3.1. Create a Web application form     Place a button on a Web Form (database2.aspx):  & The code is as follows: <asp:button id= "backupbtn" runat= "Server" text= "Backup Database" onclick= "Backupbtn_click"/>  Add a Label control:  <asp:lAbel id= "Msglabel" runat= "Server" ></asp:Label>    3.2. In the code state, add a reference     code as follows: using System;   using system.io;    3.3. Add code     code as follows://BACKUP database   protected void Backupbtn_click (object sender, EventArgs e)   {  string DbPath1, DbPath2, dbname4dbpath2;    dbname4dbpath2 = DateTime.Now.To String (). Replace (":", ".");   DBPATH1 = Server.MapPath (".. /app_data/database.mdb ");  DbPath2 = Server.MapPath (".. /app_data/"+ dbname4dbpath2 +". mdb ");    try  {  file.copy (DbPath1, DbPath2, True);    MSG Label.text = "Database backup succeeded to" + Dbname4dbpath2 + ". mdb!";  }  catch  {  msglabel.text = "Database backup failed, please try again!";   Msglabel.cssclass = "Redcolor"; } }    4. Summary     Compressed so that Microsoft Access really frees up the extra space it occupies, The database is minimized to ensure it runs most efficiently. Therefore, during the design process, it is important not to overlook the importance of compressing Microsoft Access.   recommend that you back up the database before compressing it.  
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.