首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

Access 資料庫線上壓縮修復 ASP 實作 (FSO + JRO)

     編輯:關於SqlServer

為什麼需要壓縮?

刪除資料/物件後會產生碎片,檔案膨脹、效能下降。壓縮實質為重建檔案、重組儲存、更新統計,可大幅縮減體積 (實測 3.7MB → 2.1MB)。

前置條件

  • 伺服器支援 Scripting.FileSystemObject (FSO) 權限。
  • 安裝最新 Microsoft Jet OLE DB Provider / Access Driver (含 JRO.JetEngine)。
  • 壓縮前務必備份原始檔案。

通用壓縮 ASP 程式碼

<%
' 設定來源/目標路徑 (建議壓縮到暫存檔再覆蓋)
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。

部署建議

  1. 將程式存為 compact.asp 放置於資料庫同目錄。
  2. 排程器 (Windows 排程工作) 定時呼叫 cscript compact.wsf 或 wget https://site/compact.asp。
  3. 記錄壓縮前後大小、耗時、錯誤日誌,便於追蹤。

測試環境驗證

  • Windows 98 SE + PWS
  • Windows 2000 + IIS 5.0
  • 實測 3.7MB → 2.1MB,效能明顯提升。

線上壓縮讓 Access 應用無需離線維護,配合排程自動化,持續保持資料庫健康狀態。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved