asp.net online backup, compression, and Repair Access database sample Code _ Practical Tips

Source: Internet
Author: User
Tags access database
1. The question was raised

When designing small and medium Web applications, you can choose the Microsoft Accesss as the 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. On-Line compression database implementation

2.1. Add Reference

Add a reference under the solution for the vs.net environment. 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 the Web Form (database.aspx):
Copy Code code as follows:

<asp:button id= "compactbtn" runat= "server" text= "compressed Database" onclick= "Compactbtn_click"/>
Add a Label control again:
<asp:label id= "Msglabel" runat= "Server" ></asp:Label>

2.3. Add a reference in the Code state
Copy Code code as follows:

Using System;
Using System.IO;
Using JRO;

2.4. Add code
Copy Code code as follows:

Compressing a database
protected void Compactbtn_click (object sender, EventArgs e)
{
String DbPath1, DbPath2, DbConn1, DbConn2;

DbPath1 = 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 databaseengin = new JetEngine ();
Databaseengin.compactdatabase (DBCONN1, DBCONN2);//Compression

File.Copy (DbPath2, DbPath1, true);//compress the database to overwrite the original database
File.delete (DbPath2);//Delete compressed database

Msglabel.text = "Database compression successful!";
}
Catch
{
Msglabel.text = "Database compression failed, please try again!";
}
}

3. Backing Up the database

3.1. Create a Web application form

Place a button on the Web Form (database2.aspx):
Copy Code code as follows:

<asp:button id= "backupbtn" runat= "Server" text= "Backup Database" onclick= "Backupbtn_click"/>
Add a Label control again:
<asp:label id= "Msglabel" runat= "Server" ></asp:Label>

3.2. Add a reference in the Code state
Copy Code code as follows:

Using System;
Using System.IO;

3.3. Add code
Copy Code code as follows:

Backing up a database
protected void Backupbtn_click (object sender, EventArgs e)
{
String DbPath1, DbPath2, dbname4dbpath2;

Dbname4dbpath2 = DateTime.Now.ToString (). Replace (":", ".");
DbPath1 = Server.MapPath (".. /app_data/database.mdb ");
DbPath2 = Server.MapPath (".. /app_data/"+ dbname4dbpath2 +". mdb ");

Try
{
File.Copy (DbPath1, DbPath2, true);

Msglabel.text = "Database backup succeeded to" + Dbname4dbpath2 + ". mdb!";
}
Catch
{
Msglabel.text = "Database backup failed, please try again!";
Msglabel.cssclass = "Redcolor";
}
}

4. Summary

Compression allows Microsoft Access to truly release the extra space it occupies, minimizing the database and ensuring it runs most efficiently. Therefore, during the design process, it is important not to overlook the importance of compressing Microsoft Access.
It is recommended that the database be backed up before compression.
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.