刪除資料/物件後會產生碎片,檔案膨脹、效能下降。壓縮實質為重建檔案、重組儲存、更新統計,可大幅縮減體積 (實測 3.7MB → 2.1MB)。
Scripting.FileSystemObject (FSO) 權限。JRO.JetEngine)。<%
' 設定來源/目標路徑 (建議壓縮到暫存檔再覆蓋)
Dim srcPath, dstPath, fso, jro
srcPath = Server.MapPath("data.mdb")
dstPath = Server.MapPath("data_compact.mdb")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(srcPath) Then
Response.Write "來源檔案不存在"
Response.End
End If
' 刪除舊壓縮檔
If fso.FileExists(dstPath) Then fso.DeleteFile dstPath
' 執行壓縮 (JRO 需 Jet 4.0+)
Set jro = Server.CreateObject("JRO.JetEngine")
On Error Resume Next
jro.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & srcPath, _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dstPath & ";Jet OLEDB:Engine Type=5"
If Err.Number <> 0 Then
Response.Write "壓縮失敗: " & Err.Description
Err.Clear
Else
' 覆蓋原檔
fso.DeleteFile srcPath
fso.MoveFile dstPath, srcPath
Response.Write "壓縮成功!原始大小: " & FormatNumber(fso.GetFile(srcPath).Size/1024, 1) & " KB"
End If
Set jro = Nothing
Set fso = Nothing
%>
Jet OLEDB:Engine Type=5:建立 Jet 4.0 格式 (支援 Unicode、大檔案)。Jet OLEDB:Database Password=xxx。compact.asp 放置於資料庫同目錄。cscript compact.wsf 或 wget https://site/compact.asp。線上壓縮讓 Access 應用無需離線維護,配合排程自動化,持續保持資料庫健康狀態。