- Public Function CompactAndRepair(ByVal dbPath As String, ByVal tmpFolder As String) As Boolean
- 'Declaration
- Dim strConnectionString As String
- Dim oParams() As Object
- Dim oJRO As Object = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"))
- Dim strLdbPath As String
-
- 'Checked that database exist
- If Not File.Exists(dbPath) Then
- 'File not found
- Return False
- End If
-
- 'Checked that database is not open
- strLdbPath = Mid(dbPath, 1, Len(dbPath) - 3) & "ldb"
- If File.Exists(strLdbPath) Then
- 'Database is open
- Return False
- End If
-
- 'Compact and repair
- Try
- oParams = New Object() {"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";Jet OLEDB:Engine Type=5", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tmpFolder & "~tempdb.mdb;Jet OLEDB:Engine Type=5"}
- objJRO.GetType().InvokeMember("CompactDatabase", Reflection.BindingFlags.InvokeMethod, Nothing, oJRO, oParams)
- File.Delete(dbPath)
- File.Move(tmpFolder & "~tempdb.mdb", dbPath)
-
- Runtime.InteropServices.Marshal.ReleaseComObject(oJRO)
- oJRO = Nothing
- Return True
-
- Catch ex As Exception
- Return False
- End Try
- End Function
Public Function CompactAndRepair(ByVal dbPath As String, ByVal tmpFolder As String) As Boolean
'Declaration
Dim strConnectionString As String
Dim oParams() As Object
Dim oJRO As Object = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"))
Dim strLdbPath As String
'Checked that database exist
If Not File.Exists(dbPath) Then
'File not found
Return False
End If
'Checked that database is not open
strLdbPath = Mid(dbPath, 1, Len(dbPath) - 3) & "ldb"
If File.Exists(strLdbPath) Then
'Database is open
Return False
End If
'Compact and repair
Try
oParams = New Object() {"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";Jet OLEDB:Engine Type=5", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tmpFolder & "~tempdb.mdb;Jet OLEDB:Engine Type=5"}
objJRO.GetType().InvokeMember("CompactDatabase", Reflection.BindingFlags.InvokeMethod, Nothing, oJRO, oParams)
File.Delete(dbPath)
File.Move(tmpFolder & "~tempdb.mdb", dbPath)
Runtime.InteropServices.Marshal.ReleaseComObject(oJRO)
oJRO = Nothing
Return True
Catch ex As Exception
Return False
End Try
End Function