ACCESS数据库在线压缩的实现(已经过测试)!!! 如果在 Access 数据库中删除数据或对象,或者在 Access 项目中删除对象,Access 数据库或 Access 项目可能会产生碎片并会降低磁盘空间的使用效率。压缩 Access 数据库或Access项目实际上是复制该文件,并重新组织文件在磁盘上的存储方式。压缩同时优化了 Access 数据库和 Access 项目的性能。 所以我们在对日益增大的数据库导致的ASP程序慢得无法忍受的时候,就想到了要压缩他。但常规的做法把他下载到本地然后用MSaccess来完成压缩操作,然后再上传!后来我想是否可以在线实现对ACCESS数据库的压缩呢?为此我查了一些资料,但还是不得要领,偶然在动网下载看到一篇文章,原文如下: While working on BuildDB/Buildapp online Demo, I developed a little function that will compact Access databases over the web. Here's a "no-frills" page that'll compact the databases for you. One problem with Access databases is that "holes" are created when records are deleted, making the database fluffy and bloated. Compacting the database makes it lean and efficient again. Note: This function/page can easily be combined with the Buildapp front end file navigation and search pages (Installment II), to create an application that'll make it easy to handle this formerly troublesome chore for all the databases on your machine/web site.. ++++++++++++ Begin Compact.asp +++++++++++++++++++++++++++++ <% option explicit Const JET_3X = 4
Function CompactDB(dbPath, boolIs97) Dim fso, Engine, strDBPath strDBPath = left(dbPath,instrrev(DBPath,"\")) Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(dbPath) Then Set Engine = CreateObject("JRO.JetEngine") If boolIs97 = "True" Then Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb;" _ & "Jet OLEDB:Engine Type=" & JET_3X Else Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb" End If fso.CopyFile strDBPath & "temp.mdb",dbpath fso.DeleteFile(strDBPath & "temp.mdb") Set fso = nothing Set Engine = nothing CompactDB = "Your database, " & dbpath & ", has been Compacted" & vbCrLf Else CompactDB = "The database name or path has not been found. Try Again" & vbCrLf End If End Function %> <html><head><title>Compact Database</title></head><body> <h2 align="center"> Compacting an Access database</h2> <p align="center"> <form action=compact.asp> Enter relative path to the database, including database name.<br><br> <input type="text" name="dbpath"><br><br> <input type="checkbox" name="boolIs97" value="True"> Check if Access 97 database <br><i> (default is Access 2000)</i><br><br> <input type="submit"> <form> <br><br> <% Dim dbpath,boolIs97 dbpath = request("dbpath") boolIs97 = request("boolIs97") If dbpath <> "" Then dbpath = server.mappath(dbpath) response.write(CompactDB(dbpath,boolIs97)) End If %> </p></body></html> ++++++++++++ End Code 我在本机测试获得成功,又上网对自己的动网论坛实施了压缩操作结果也完全正常,数据库从原来的3.7M压缩到了2.1M从而证实是可行的。需要注意的是:本程序其实是通过FSO权限和JET引擎连接,因此在使用之前请确认你的服务器支持FSO(filesystemobject)权限并安装的ACCESS最新驱动!从安全出发,请在压缩之前备份原始数据库!已经测试通过的运行环境: WIN98SE+PWS、WIN2000+IIS5.0 下面是我整理后的源代码,复制另存为compact.asp并上传到数据库所在目录即可正常使用。 <html> <head> <title>ACCESS数据库压缩程序</title> </head> <body bgcolor="e0f8ef"> <div> <div align="center"><font color="#3300FF"><b><font size="5">通用ACCESS数据库在线压缩程序</font></b></font><br> </div> <div><br> 本程序其实是通过FSO权限和JET引擎连接,因此在使用之前请确认你的服务器支持FSO(filesystemobject)权限并安装的ACCESS最新驱动!从安全出发,请在压缩之前备份原始数据库!</div><br> <div align="center">运行环境:在WIN98SE+PWS、WIN2000+IIS5.0 <br> <% Const JET_3X = 4 Function CompactDB(dbPath, boolIs97) Dim fso, Engine, strDBPath strDBPath = left(dbPath,instrrev(DBPath,"\")) Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(dbPath) Then Set Engine = CreateObject("JRO.JetEngine") If boolIs97 = "True" Then Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb;" _ & "Jet OLEDB:Engine Type=" & JET_3X Else Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb" End If fso.CopyFile strDBPath & "temp.mdb",dbpath fso.DeleteFile(strDBPath & "temp.mdb") Set fso = nothing Set Engine = nothing CompactDB = "你的数据库, " & dbpath & ", 已经被压缩" & vbCrLf Else CompactDB = "你输入的数据库路径或名称未找到,请重试" & vbCrLf End If End Function %> </div> </div> <form name="compact" method="post" action="compact.asp"> <div align="center"><font size="2"><b><font color="#FF0000">压缩选项,请仔细填写!</font></b><br> <br> 输入数据库全称: <input type="text" name="dbpath"> (包括扩展名,如MDB、ASA、ASP等)<br> <br> <input type="checkbox" name="boolIs97" value="True"> 检查是否为ACCESS97数据库<br> (默认为ACCESS2000的数据库)<br> <br> <input type="submit" name="submit" value="确认压缩"> </font></div> </form> <div align="center"><font size="2"> <% Dim dbpath,boolIs97 dbpath = request("dbpath") boolIs97 = request("boolIs97") If dbpath <> "" Then dbpath = server.mappath(dbpath) response.write(CompactDB(dbpath,boolIs97)) End If %> <br> </font></div>
|