Accessデータベースをプログラムで(ADOXを使用して、OleDbConnectionを使用して)「圧縮および修復」することは可能ですか?
7 に答える
以前の「回答」に返信するのに十分な担当者がいませんが、OP の質問に関して他の誰かに役立つ情報を提供したかったのです。
私は何年もの間、VB.net から Access 2000 データベースを圧縮/修復するために JRO メソッドを使用してきました。ブルームーンに一度、データベースを破損させたクライアントがあります(通常、ネットワーク経由でデータベースに接続し、予期しないネットワークの中断が発生した場合)。JRO (私の経験では) は、データベースが破損していない限り問題なく動作します。Accessアプリケーションを使用してデータベースを修復できる理由がわかりませんでしたが、MYアプリケーション(JROを使用)を使用すると、圧縮/修復は常に失敗します(データベースは認識されない形式です)。
そのため、ちょうど 1 時間前にこのスレッドに出くわした後、DAO への参照をアプリにドロップし、破損したデータベースを修復する機能を試してみました。 )。JRO が失敗したとき、DAO はデータベースを修復できたと思います!
OK、これが JRO と DAO の私の経験です。それが役に立てば幸い。CompactDatabase
DAO から使用するためのサンプル コードを次に示します。
Dim dbCorrupt As String = "c:\CorruptedDB.mdb"
Dim dbRepaired As String = Path.Combine(Path.GetDirectoryName(dbPath), Path.GetFileNameWithoutExtension(dbPath) & "_Repaired.mdb")
Dim dao As New dao.DBEngine
dao.CompactDatabase(dbCorrupt, dbRepaired)
c#.net のわずか 4 行のコードです。
最初にライブラリを使用します。
using JRO;
test.mdb
次のコードで圧縮して修復します。
string currentdirectory = System.IO.Directory.GetCurrentDirectory();
string oldmdbfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + currentdirectory + "\\test.mdb;Jet OLEDB:Database Password='xyz'";
string newmdbfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + currentdirectory + "\\test1.mdb;Jet OLEDB:Database Password='xyz'";
string oldmdbfilepath = currentdirectory + "\\test.mdb";
string newmdbfilepath = currentdirectory + "\\test1.mdb";
JRO.JetEngine engine = new JetEngine();
engine.CompactDatabase(oldmdbfile, newmdbfile);
File.Delete(oldmdbfilepath);
File.Move(newmdbfilepath, oldmdbfilepath);
MessageBox.Show("Database compact and repaired successfully !",);
したがってtest.mdb
、圧縮および修復され、新しいファイルtest1.mdb
が作成されます。次に、削除test.mdb
して名前test1.mdb
を に変更するだけtest.mdb
です。
次の 2 つの方法で、MS ACCESS データベースを圧縮および修復できます。
- DAOの使用:DAO350にはメソッドがあり
RepairDatabase()
、DAO360にはメソッドがありますCompactDatabase()
- MDAC+JRO を使用:
例として、VB6 (古い、古い、古い...) では次のようにします。
Dim jro As jro.JetEngine
Set jro = New jro.JetEngine
jro.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db_to_repair.mdb;Jet OLEDB:Database Password=mypass", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\repaired_db.mdb;Jet OLEDB:Engine Type=4;Jet OLEDB:Database Password=mypass"
お気づきのとおり、この関数では、修復するデータベースの名前と修復されたデータベースの名前を指定する必要があります。
VBScriptのサンプルコード。
Dim objEngine
Dim objProcess
'Dim objDB
Dim strDb1
Dim strPath
Dim strFile
Dim strDAOversion
Dim strApplicationName
Dim strErr
Dim strMsg
Dim FSO
strPath = "C:\Docs\"
strFile = "Some.mdb"
strDb1 = strPath & strFile
Set FSO=CreateObject("Scripting.FileSystemObject")
strDAOversion = "DAO.DBEngine.36"
strApplicationName = "Some.mdb"
strMsg = "About to perform a COMPACT on "
strMsg = strMsg & chr(10) & chr(10)
strmsg = strMsg & strApplicationName
strMsg = strMsg & chr(10) & chr(10)
strmsg = strmsg & "Please ask everyone to EXIT THE SYSTEM."
strMsg = strmsg & chr(10) & chr(10)
strmsg = strmsg & space(12) & "It is VITAL you do not exit windows until"
strMsg = strMsg & chr(10)
strMsg = strMsg & space(12) & "you receive the confirmation message."
strMsg = strmsg & chr(10) & chr(10)
strMsg = strMsg & space(6) & "Press OK to continue or Cancel to stop the process."
If MsgBox(strMsg, 1, strApplicationName) = 1 Then
Set objEngine = WScript.CreateObject(strDAOversion)
Call CompactDB(FSO, objEngine, strDb1, "password")
If strErr="True" Then
strMsg = "Please correct the problem and try again."
MsgBox strMsg, 1, strApplicationName
Else
strMsg = "Database compacting complete."
MsgBox strMsg, 1, strApplicationName
End If
End If
Function CompactDB(objFSO, objEngine, strDb, pwd)
'Compact the database
Dim strdbtemp
Dim MsgText
strdbtemp = Left(strDb, Len(strDb) - 3) & "ldb"
If FSO.FileExists(strdbtemp) = True Then 'if ldb file exists, db is still open.
MsgText = "You have not exited the file. Please close and try again."
MsgBox MsgText, 1, strApplicationName
strErr="True"
Exit Function
End If
If FSO.FileExists(strDb1) = False Then
MsgText = "Cannot locate the database at " & strDB
MsgBox MsgText, 1, strApplicationName
strErr="True"
Exit Function
End If
strdbtemp = Left(strDb, Len(strDb) - 3) & "tmp"
If pwd = "" Then
objEngine.CompactDatabase strDb, strdbtemp
Else
objEngine.CompactDatabase strDb, strdbtemp, , , ";pwd=" & pwd
End If
If Err = 0 Then
FSO.deletefile strDb
FSO.copyfile strdbtemp,strDb
FSO.deletefile strdbtemp
Else
MsgText = "Error during COMPACT process for " & strDB
MsgBox MsgText, 1, strApplicationName
strErr="True"
End If
End Function
これは公式の MS リンクです。これ以上のコメントは冗長です。 DBEngine.CompactDatabase メソッド
参照を追加: Microsoft ActiveX Data Objects 2.x Library Microsoft Jet and Replication Objects 2.x Library
sDB = "c:\DB\myDb.mdb"
sDBtmp = "c:\DB\tempMyDb.mdb"
sPASSWORD = "password"
Dim oApp As Access.Application
Set oApp = New Access.Application
Call oApp.DBEngine.CompactDatabase(sDB, sDBtmp, dbLangGeneral, , ";pwd=" & sPASSWORD)
'wait for the app to finish
DoEvents
'remove the uncompressed original
Kill sDB
'rename the compressed file to the original to restore for other functions
Name sDBtmp As sDB