Click here to Skip to main content
15,897,704 members
Articles / Web Development / ASP.NET

Compact and Repair an Access Database Programmatically Using C#

Rate me:
Please Sign up or sign in to vote.
3.84/5 (14 votes)
20 Mar 2007CPOL2 min read 113.4K   1.9K   40  
A simple method that compacts and repairs a Microsoft Access database file using the Microsoft Jet library and C# COM interop. The example code is meant to be integrated into an ASP.NET web site.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using JRO;


/// <summary>
/// Encapsulates small static utility functions.
/// </summary>
public class Utility
{

 /// <summary>The connection to use to connect to an Access database using JET.</summary>
 public const string AccessOleDbConnectionStringFormat = "Data Source={0};Provider=Microsoft.Jet.OLEDB.4.0;";

 /// <summary>
 /// Compacts an Access database using Microsoft JET COM interop.
 /// </summary>
 /// <param name="fileName">
 /// The filename of the Access database to compact. This
 /// filename will be mapped to the appropriate path on the web server, so use a
 /// tilde (~) to specify the web site root folder. For example, "~/Downloads/Export.mdb".
 /// The ASP.NET worker process must have been granted permission to read and write
 /// this file, as well as to create files in the folder in which this file resides.
 /// In addition, Microsoft JET 4.0 or later must be present on the server.
 /// </param>
 /// <returns>
 /// True if the compact was successful. False can indicate several possible problems
 /// including: unable to create JET COM object, unable to find source file, unable to create
 /// new compacted file, or unable to delete original file.
 /// </returns>
 public static bool CompactJetDatabase(string fileName)
 {
  // I use this function as part of an AJAX page, so rather than throwing
  // exceptions if errors are encountered, I simply return false and allow the page
  // to handle the failure generically.
  try
  {
   // Find the database on the web server
   string oldFileName = HttpContext.Current.Server.MapPath(fileName);

   // JET will not compact the database in place, so we need to create a temporary filename to use
   string newFileName = Path.Combine(Path.GetDirectoryName(oldFileName), Guid.NewGuid().ToString("N") + ".mdb");

   // Obtain a reference to the JET engine
   JetEngine engine = (JetEngine)HttpContext.Current.Server.CreateObject("JRO.JetEngine");

   // Compact the database (saves the compacted version to newFileName)
   engine.CompactDatabase(
    String.Format(AccessOleDbConnectionStringFormat, oldFileName),
    String.Format(AccessOleDbConnectionStringFormat, newFileName));

   // Delete the original database
   File.Delete(oldFileName);

   // Move (rename) the temporary compacted database to the original filename
   File.Move(newFileName, oldFileName);

   // The operation was successful
   return true;
  }
  catch
  {
   // We encountered an error
   return false;
  }
 }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions