赶知识网

asp用fso实现压缩access数据库

2013-10-25 / 3014次点击 asp/access/IIS/mssql
curdbpath ="data/CMS.mdb" '当前正在使用的数据库
tempdb = "temp/temp.mdb" '生成临时数据库,
compactdb="temp/compact.mdb" '压缩后的数据库

set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile server.MapPath(curdbpath),server.MapPath(tempdb),true ‘通常当前数据库正在使用,所以先复制一个出来才能进行压缩

Set Engine = Server.CreateObject("JRO.JetEngine")
dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Engine.CompactDatabase dbProvider & server.MapPath(tempdb), dbProvider & server.MapPath(compactdb) '将复制的临时数据库进行压缩
set Engine = nothing

   fso.CopyFile server.MapPath(compactdb),server.MapPath(curdbpath),true '把压缩后的数据库复制过去覆盖当前正在使用的数据库,复制覆盖是可以操作的

fso.deletefile(server.MapPath(tempdb))'删除临时文件

fso.deletefileserver.MapPath(compactdb)'删除压缩后的临时文件,可以直接将上面的copyfile改成movefile就不用删除了

我把我的源文件给你,你自己适当修改下就是你自己的了:
<!-- #include file="../conn2.asp" -->
<!-- #include file="../inc/md5.asp" -->
<html>
<head>
<title>数据库压缩</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../style/style.css" rel="stylesheet" type="text/css">
<style>
body {
background-color:#FFFFFF;
}
</style>
</HEAD>

<BODY>
<%if request("hid1")="" then%>
<form name="form1" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#C4D8ED">
<tr>
<td><img src="../images/r_1.gif" alt="" /></td>
<td width="100%" background="../images/r_0.gif">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td> 压缩数据库</td>
<td align="right"> </td>
</tr>
</table>
</td>
<td><img src="../images/r_2.gif" alt="" /></td>
</tr>
<tr>
<td></td>
<td>
<table align="center" cellpadding="4" cellspacing="1" class="toptable grid" border="1">
<tr>
<td height="30" colspan="2" align="center" class="category">你的空间只有支持fso才可以进行如下操作,否则你只能手动压缩</td>
</tr>
<tr>
<td width="25%" height="30" align="right">当前数据库路径:</td>
<td width="75%" class="category">
<input type="text" name="mdbpath" size="30" value="backup/要压缩的数据库名称.mdb">
<input type="radio" value="false" checked name="ys"> 使用 Access 2000 数据库
</td>
</tr>
<tr>
<td height="30" align="right"> </td>
<td class="category">
(为了安全,请注意用备份的数据库文件来压缩,不要用当前正在运行的数据库文件)
</td>
</tr>
<tr class="a3">
<td align="center" colspan="2" class="category">
<input type="submit" value=" 确认压缩 " class="button">
<input type="hidden" name="hid1" value="ok"></td>
</tr>
</table>
</td>
<td></td>
</tr>
<tr>
<td><img src="../images/r_4.gif" alt="" /></td>
<td></td>
<td><img src="../images/r_3.gif" alt="" /></td>
</tr>
</table>
</form>
<%
else
'-----------------------------------------
Dim dbpath,boolIs97

dbpath="../"&request.form("mdbpath")
boolIs97=request.form("ys")

If dbpath <> "" Then
dbpath = server.mappath(dbpath)
response.write(CompactDB(dbpath,boolIs97))
End If

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
response.write"<script>alert('您的数据库已经压缩成功!');history.go(-1)</script>"
response.End()
Else
response.write"<script>alert('数据库名称或路径不正确. 请重试!');history.go(-1)</script>"
response.End()
End If

End Function
'--------------------------------------------
end if
%>
</body>
</html>
有用 0 没用 0

Top10

沪ICP备09053415号 © 赶知识网