Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Visual Basic
Article

How to Compact Microsoft Access Database Using Visual Studio.NET

Rate me:
Please Sign up or sign in to vote.
4.45/5 (13 votes)
8 Jun 2006CPOL2 min read 122K   1.8K   34   14
A way to automatically compact Microsoft Access database

Introduction

When you use Microsoft Access database, you may have noticed that file size always keeps growing and you can never decrease the size. Even if you delete some entries and tables, the size remains the size or sometimes grows despite the deletion. You then need to compact your database. Using the compact utility, the file size decreases to a large extent.

Background

Microsoft Access database creates this situation when you use an open connection very frequently or open the connection several times to communicate with the database. The very definite reason of size increasing is that your database is really of large size and is increasing. All three situations can be handled using the compact utility. In my previous article, I describe how to create and populate a Microsoft Access file. While doing this, I came to know that the file size is really so large and used the compact feature to reduce the size. It was an amazingly big difference, so then I decided to compact that automatically.

Requirements

For the development of this utility, you need to add a reference to the DOA Library. DOA library provides the functionality to compact the database. One more thing that may be interesting is the database file that you want to compact should be of large size, otherwise you will not feel the effect.

Code

VB

VB.NET
Imports System.IO
Module Module1
    Sub Main()
        Try
            Dim File_Path, compact_file As String
            'Original file path that u want to compact
            File_Path = AppDomain.CurrentDomain.BaseDirectory & "db.mdb"
            'compact file path, a temp file
            compact_file = AppDomain.CurrentDomain.BaseDirectory & "db1.mdb"
            'First check the file u want to compact exists or not
            If File.Exists(File_Path) Then
                Dim db As New DAO.DBEngine()
                'CompactDatabase has two parameters, creates a copy of 
                'compact DB at the Destination path
                db.CompactDatabase(File_Path, compact_file)
            End If
            'restore the original file from the compacted file
            If File.Exists(compact_file) Then
                File.Delete(File_Path)
                File.Move(compact_file, File_Path)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

C#

C#
using System.IO; 
struct Module1 
{ 

 void Main() 
 { 
   try { 
     string File_Path; 
     string compact_file; 
     File_Path = AppDomain.CurrentDomain.BaseDirectory + "db.mdb"; 
     compact_file = AppDomain.CurrentDomain.BaseDirectory + "db1.mdb"; 
     if (File.Exists(File_Path)) { 
       DAO.DBEngine db = new DAO.DBEngine(); 
       db.CompactDatabase(File_Path, compact_file);  
     } 
     if (File.Exists(compact_file)) { 
       File.Delete(File_Path);
       File.Move(compact_file, File_Path); 
     } 
   } catch (Exception ex) { 
     MsgBox(ex.Message); 
   } 
 } 
}

Points of Interest

Using this utility, you can schedule a job to compact your database. But remember to schedule this at some specific time when you feel the database may not be in use as this compact utility deletes the original file and restores it with the compact one.

Summary

To use this utility, you do not need to do it yourself. Rather, you only need to schedule a job and relax. Happy programming.

History

  • 9th June, 2006: Initial post

License

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


Written By
Program Manager ZonG, China Mobile Company
Pakistan Pakistan
I am working as Manager Enterprise Services where key responsibilities are to support end users on their day to day IT related issues.
The other function I am responsible is IT IPCC Services Support Since September 2013 and my key responsibilities are to maintain IPCC Uptime along with end users support at both CMPak call centers. IPCC enhancements and new feature development is also part of my job role.
The third area that was handed over to me in March 2015 is in-house apps support, where key responsibility is to provide support for all the application developed by IT department and to maintain the availability of these applications.
Previously I was working as Manager Automation & Planning and this is my key area of expertise. My responsibilities were to manage team in Project Planning, System Analysis, Requirements gathering, Preparation of SRS, Presentations to client about the requirements, getting Approvals on SRS from the Client, Database Design, Development Tracking, System Deployments, end user Feedback along with Change Management.

Planning and Defining Scope, Resource Planning, Time Estimating, Creating Charts and Schedules, Risk Analysis
Managing Risks and Issues, Monitoring and Reporting Progress, Team Leadership, Working with Vendors, Scalability were the key area of working along with an efficient problem-solver in professional way with envisions business and technical perspectives to develop workable solutions.

I started my career as developer in 2004 and promoted as team lead IT Automation in 2007 and then as manager so I spend 7 years in Software Development & Project Planning.

Comments and Discussions

 
AnswerThanks Pin
Member 1479809310-Apr-20 3:56
Member 1479809310-Apr-20 3:56 
QuestionThanks Pin
Member 130475887-Apr-20 20:14
Member 130475887-Apr-20 20:14 
GeneralThank you very much Pin
ramugaru9-Dec-06 7:54
ramugaru9-Dec-06 7:54 
GeneralI've found a solution, here it is [modified] Pin
Martin Stevens16-Oct-06 22:43
Martin Stevens16-Oct-06 22:43 
GeneralAdditional note about adding the JRO object Pin
Martin Stevens9-Dec-06 21:37
Martin Stevens9-Dec-06 21:37 
I did it using visual studio 2005. It should also work with the express versions of visual basic and visual web developer.

1) Open your project in Visual Studio .NET
2) Right click References in the Solution Explorer and select 'Add Reference...'
3) Click the COM tab and select 'Microsoft Jet and Replication Objects 2.x Library' 'x' will be the version you have.
4) Click the Select button and then Ok.

Now you should have references to ADODB and JRO added to your project. This will drop the Interop.JRO.dll in your bin folder and you should be in business using the code I posted.

You may also need to look at importing the following name spaces if you havent already done so in your web config.

System

System.IO

System.Configuration;



Martijn
Generalnice job Pin
Kali Babu12315-Jun-06 21:24
Kali Babu12315-Jun-06 21:24 
QuestionIs Interop.DAO.dll part of VS2005 Pin
*chopper*13-Jun-06 21:11
*chopper*13-Jun-06 21:11 
AnswerRe: Is Interop.DAO.dll part of VS2005 Pin
Malik Nasir13-Jun-06 23:29
Malik Nasir13-Jun-06 23:29 
GeneralRe: Is Interop.DAO.dll part of VS2005 Pin
*chopper*14-Jun-06 1:03
*chopper*14-Jun-06 1:03 
GeneralRe: Is Interop.DAO.dll part of VS2005 [modified] Pin
Zeeshan Sadiq19-Aug-07 16:06
Zeeshan Sadiq19-Aug-07 16:06 
GeneralCheck Pin
Steve Hansen8-Jun-06 22:26
Steve Hansen8-Jun-06 22:26 
GeneralRe: Check Pin
Malik Nasir8-Jun-06 22:30
Malik Nasir8-Jun-06 22:30 
GeneralRe: Check Pin
DBuckner9-Jun-06 5:50
DBuckner9-Jun-06 5:50 
GeneralRe: Check Pin
BloodBaz12-Jun-06 2:48
BloodBaz12-Jun-06 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.