Compact and Repair an Access Database. Add Ref. to : AdoDb, Jro



< ?xml version="1.0" encoding="utf-8" ?>









using ADODB;
using JRO;
using System.Configuration;
using System.Data.OleDb;
using System.IO;

public class CompactAndRepairAccessDb : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private JRO.JetEngine jro;
private System.Windows.Forms.Button btnConfirm;
private System.Windows.Forms.TextBox tbxOriginalDbSize;
private System.Windows.Forms.TextBox tbxCompactedDbSize;
private OleDbConnection cnn;

public CompactAndRepairAccessDb() {
InitializeComponent();

FileInfo fi = new FileInfo( ConfigurationSettings.AppSettings["PathOriginal"] );
int s = Convert.ToInt32( fi.Length/1000 );
this.tbxOriginalDbSize.Text = s.ToString() + " kb";
}

private void btnConfirm_Click(object sender, System.EventArgs e) {
// First close all instances of the database
// MUST HAVE EXCLUSIVE use of the DB by the app
//
cnn = new OleDbConnection( ConfigurationSettings.AppSettings["CnnStrOriginal"] );
cnn.Open();
cnn.Close();

// Compact DB
//
if( cnn.State.ToString() == "Closed" ) {
jro = new JRO.JetEngine();
jro.CompactDatabase( ConfigurationSettings.AppSettings["CnnStrOriginal"],ConfigurationSettings.AppSettings["CnnStrCompacted"] );

FileInfo fi = new FileInfo( ConfigurationSettings.AppSettings["PathCompacted"] );
int s = Convert.ToInt32( fi.Length/1000 );
this.tbxCompactedDbSize.Text = s.ToString() + " kb";

// Delete OldDb | Rename the Compacted DB
//
File.Delete( ConfigurationSettings.AppSettings["PathOriginal"] );
File.Copy( ConfigurationSettings.AppSettings["PathCompacted"], ConfigurationSettings.AppSettings["PathOriginal"] );
File.Delete( ConfigurationSettings.AppSettings["PathCompacted"] );
}
}
}

No comments:

Ramadan - What is it?

  Ramadan is one of the most important and holy months in the Islamic calendar. It is a time of fasting, prayer, and spiritual reflection fo...