明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

純編碼完成Access數(shù)據(jù)庫的創(chuàng)建或壓縮

[摘要]<% '#######以下是一個類文件,下面的注解是調(diào)用類的方法################################## '# 注意:如果系統(tǒng)不支持建立Script...
<%
'#######以下是一個類文件,下面的注解是調(diào)用類的方法##################################
'# 注意:如果系統(tǒng)不支持建立Scripting.FileSystemObject對象,那么數(shù)據(jù)庫壓縮功能將無法使用
'# Access 數(shù)據(jù)庫類
'# CreateDbFile 建立一個Access 數(shù)據(jù)庫文件
'# CompactDatabase 壓縮一個Access 數(shù)據(jù)庫文件
'# 建立對象方法:
'# Set a = New DatabaseTools
'# by (蕭寒雪) s.f.
'##################################

Class DatabaseTools

Public function CreateDBfile(byVal dbFileName,byVal DbVer,byVal SavePath)
'建立數(shù)據(jù)庫文件
'If DbVer is 0 Then Create Access97 dbFile
'If DbVer is 1 Then Create Access2000 dbFile
On error resume Next
If Right(SavePath,1)<>"\" Or Right(SavePath,1)<>"/" Then SavePath = Trim(SavePath) & "\"
If Left(dbFileName,1)="\" Or Left(dbFileName,1)="/" Then dbFileName = Trim(Mid(dbFileName,2,Len(dbFileName)))
If DbExists(SavePath & dbFileName) Then
Response.Write ("對不起,該數(shù)據(jù)庫已經(jīng)存在!")
CreateDBfile = False
Else
Dim Ca
Set Ca = Server.CreateObject("ADOX.Catalog")
If Err.number<>0 Then
Response.Write ("無法建立,請檢查錯誤信息<br>" & Err.number & "<br>" & Err.Description)
Err.Clear
Exit function
End If
If DbVer=0 Then
call Ca.Create("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & SavePath & dbFileName)
Else
call Ca.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SavePath & dbFileName)
End If
Set Ca = Nothing
CreateDBfile = True
End If
End function

Public function CompactDatabase(byVal dbFileName,byVal DbVer,byVal SavePath)
'壓縮數(shù)據(jù)庫文件
'0 為access 97
'1 為access 2000
On Error resume next
If Right(SavePath,1)<>"\" Or Right(SavePath,1)<>"/" Then SavePath = Trim(SavePath) & "\"
If Left(dbFileName,1)="\" Or Left(dbFileName,1)="/" Then dbFileName = Trim(Mid(dbFileName,2,Len(dbFileName)))
If DbExists(SavePath & dbFileName) Then
Response.Write ("對不起,該數(shù)據(jù)庫已經(jīng)存在!")
CompactDatabase = False
Else
Dim Cd
Set Cd =Server.CreateObject("JRO.JetEngine")
If Err.number<>0 Then
Response.Write ("無法壓縮,請檢查錯誤信息<br>" & Err.number & "<br>" & Err.Description)
Err.Clear
Exit function
End If
If DbVer=0 Then
call Cd.CompactDatabase("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & SavePath & dbFileName,"Provider=Microsoft.Jet.OLEDB.3.51;Data
Source=" & SavePath & dbFileName & ".bak.mdb;Jet OLEDB;Encrypt Database=True")
Else
call Cd.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
SavePath & dbFileName,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
SavePath & dbFileName & ".bak.mdb;Jet OLEDB;Encrypt Database=True")
End If
'刪除舊的數(shù)據(jù)庫文件
call DeleteFile(SavePath & dbFileName)
'將壓縮后的數(shù)據(jù)庫文件還原
call RenameFile(SavePath & dbFileName & ".bak.mdb",SavePath & dbFileName)
Set Cd = False
CompactDatabase = True
End If
end function

Public function DbExists(byVal dbPath)
'查找數(shù)據(jù)庫文件是否存在
On Error resume Next
Dim c
Set c = Server.CreateObject("ADODB.Connection")
c.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath
If Err.number<>0 Then
Err.Clear
DbExists = false
else
DbExists = True
End If
set c = nothing
End function

Public function AppPath()
'取當(dāng)前真實路徑
AppPath = Server.MapPath("./")
End function

Public function AppName()
'取當(dāng)前程序名稱
AppName = Mid(Request.ServerVariables("SCRIPT_NAME"),(InStrRev(Request.ServerVariables("SCRIPT_NAME") ,"/",-1,1))+1,Len(Request.ServerVariables("SCRIPT_NAME")))
End Function

Public function DeleteFile(filespec)
'刪除一個文件
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.number<>0 Then
Response.Write("刪除文件發(fā)生錯誤!請查看錯誤信息<br>" & Err.number & "<br>" & Err.Description)
Err.Clear
DeleteFile = False
End If
call fso.DeleteFile(filespec)
Set fso = Nothing
DeleteFile = True
End function

Public function RenameFile(filespec1,filespec2)
'修改一個文件
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.number<>0 Then
Response.Write("修改文件名時發(fā)生錯誤!請查看錯誤信息<br>" & Err.number & "<br>" & Err.Description)
Err.Clear
RenameFile = False
End If
call fso.CopyFile(filespec1,filespec2,True)
call fso.DeleteFile(filespec1)
Set fso = Nothing
RenameFile = True
End function

End Class
%>