Compact and repair mdb

StewartBW 745 Reputation points
2024-06-17T00:32:04.99+00:00

Hello

I use Access Database Engine and OleDbReader to work with mdb files.

If my memory serves me, I've been told, compact (and repair if necessary) of mdb files is not provided in Access Database Engine?

If so, which component and command to use, when Office is not installed on target systems, so must ship the dll with app.

Thanks.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 82,671 Reputation points
    2024-06-17T06:15:17.1433333+00:00
    0 comments No comments

  2. Jiachen Li-MSFT 28,001 Reputation points Microsoft Vendor
    2024-06-17T06:59:11.3633333+00:00

    Hi @StewartBW ,

    You can try using the Microsoft.Jet.OLEDB.4.0 provider and the JRO (Jet and Replication Objects) library, which provides methods for compacting and repairing Access databases.

    Right-click on your project in Visual Studio and select "Add Reference". Go to the "COM" tab. Find and select Microsoft Jet and Replication Objects 2.6 Library

    Imports System.Runtime.InteropServices
    Imports JRO
    
    
            Dim sourceFile As String = "C:\path\to\your\database.mdb"
            Dim tempFile As String = "C:\path\to\your\temp_database.mdb"
    
            Try
                CompactAndRepair(sourceFile, tempFile)
                Console.WriteLine("Database compacted and repaired successfully.")
            Catch ex As Exception
                Console.WriteLine($"Error: {ex.Message}")
            End Try
        End Sub
    
        Public Sub CompactAndRepair(sourceFile As String, tempFile As String)
            Dim jetEngine As New JRO.JetEngine()
    
            Dim connectionString As String = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={sourceFile}"
            Dim tempConnectionString As String = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={tempFile}"
    
            jetEngine.CompactDatabase(connectionString, tempConnectionString)
    
            ' Replace the original file with the compacted one
            System.IO.File.Delete(sourceFile)
            System.IO.File.Move(tempFile, sourceFile)
    
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments