![]() |
Tipp 0063
|
Access-Datenbanken reparieren/komprimieren
|
 |
|
Autor/Einsender: Datum: |
|
Berhard Froschauer 07.12.2004 |
|
Entwicklungsumgebung: |
|
VB.Net 2003 |
Framework: |
|
1.1 |
|
|
In Microsoft Access existiert im Menü Extras/Datenbank-Dienstprogramme die Möglichkeit,
eine defekte Datenbank zu reparieren. Das selbe kann auch kann auch direkt über JRO-Code
erreicht werden, wie dieses Beispiel zeigt.
|
Der JRO-Com-Library Microsoft Jet and Replication Objects 2.6 Library wird über
einen Verweis eingebunden und kann über die Methode CompactDatabase die Reparatur
durchführen.
|
|
|
Imports System.Reflection
Imports System.IO
Imports JRO
Friend Class anwendungspfad
Public Shared Function GetPath() As String
Dim oMod As System.Reflection.Module = _
[Assembly].GetExecutingAssembly().GetModules()(0)
Dim pfad As String
Dim len1 As Integer
len1 = oMod.Name.Length
pfad = oMod.FullyQualifiedName
pfad = pfad.Remove(pfad.Length - len1, len1)
If Not pfad.EndsWith("\") Then
pfad += "\"
End If
Return pfad
End Function
End Class
Public Class repair_compact
Public Shared Function compactAccessDB(ByVal DbFilname As _
String, Optional ByVal savePfad As String = "") As String
Dim source, destination As String
Dim ApplicationPfad As String = anwendungspfad.GetPath
Dim originalMDB As String = DbFilname
Dim tmpDB As String
Try
If savePfad.Equals("") Then
savePfad = ApplicationPfad
End If
savePfad = Pfadok(savePfad)
tmpDB = savePfad & "tmp.mdb"
File.Move(DbFilname, tmpDB)
source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
tmpDB & " ;"
destination = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DbFilname & " ;"
Dim jro As New JRO.JetEngine
jro.CompactDatabase(source, destination)
File.Delete(tmpDB)
compactAccessDB = "Datenbank " & Environment.NewLine & _
"erfolgreich repariert und komprimiert!"
Catch ex As System.IO.IOException
compactAccessDB = ex.Message
Catch ex As System.Runtime.InteropServices.COMException
compactAccessDB = ex.Message
If File.Exists(tmpDB) Then
File.Move(tmpDB, DbFilname)
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
If File.Exists(tmpDB) Then
File.Move(tmpDB, DbFilname)
End If
Throw ex
End Try
End Function
Friend Shared Function Pfadok(ByVal pfad As String) As String
If Not pfad.EndsWith("\") Then
pfad += "\"
End If
Return pfad
End Function
End Class
|
|
|
|
|
Windows-Version |
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
|
Download (9,5 kB)
|
Downloads bisher: [ 1409 ]
|
|
|