Database creation or compression using pure Encoding

Source: Internet
Author: User
% ####### The following is a class file, and the annotation below is the method of calling the class # Note: if the system does not support Scripting. for FileSystemObject objects, the database compression function cannot be used # access database class # CreateDbFile creates an Access database file # CompactDatabase compresses an Acce

% '####### The following is a class file, and the annotation below is the method for calling the class' # Note: if the system does not support Scripting. fileSystemObject object, the database compression function will not be able to use '# access Database Class' # CreateDbFile to create an Access database file '# CompactDatabase to compress an Acce

<%
####### The following is a class file, and the annotation below is the method of calling the class
'# Note: if the system does not support CreateScripting. FileSystemObject object,
So Database CompressionFunction will be unavailable
'# Access DatabaseClass
'# CreateDbFile CreateOne Access DatabaseFile
'# CompactDatabase CompressionOne Access DatabaseFile
'# CreateObject method:
'# Set a = New DatabaseTools


Class DatabaseTools

Public function CreateDBfile (byVal dbFileName, byVal DbVer, byVal SavePath)
' Create DatabaseFile
'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 ("sorry, this DatabaseAlready exists! ")
CreateDBfile = False
Else
Dim Ca
Set Ca = Server. CreateObject ("ADOX. Catalog ")
If Err. number <> 0 Then
Response. Write ("No Create, Please check the error message
"& Err. number &"
"& 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)
' Compression DatabaseFile
'0 is access 97
'1 is 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 ("sorry, this DatabaseAlready exists! ")
CompactDatabase = False
Else
Dim Cd
Set Cd = Server. CreateObject ("JRO. JetEngine ")
If Err. number <> 0 Then
Response. Write ("No Compression, Please check the error message
"& Err. number &"
"& 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
'Delete the old DatabaseFile
Call DeleteFile (SavePath & dbFileName)
'Convert CompressionAfter DatabaseFile Restoration
Call RenameFile (SavePath & dbFileName & ". bak. mdb", SavePath & dbFileName)
Set Cd = False
CompactDatabase = True
End If
End function

Public function DbExists (byVal dbPath)
'Lookup DatabaseWhether the file exists
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 ()
'Get the current real path
AppPath = Server. MapPath ("./")
End function

Public function AppName ()
'Get the name of the current program
AppName = Mid (Request. serverVariables ("SCRIPT_NAME"), (Request. serverVariables ("SCRIPT_NAME"), "/",-1, 1) + 1, Len (Request. serverVariables ("SCRIPT_NAME ")))
End Function

Public function DeleteFile (filespec)
'Delete an object
Dim fso
Set fso = CreateObject ("Scripting. FileSystemObject ")
If Err. number <> 0 Then
Response. Write ("An error occurred while deleting the file! View error information
"& Err. number &"
"& Err. Description)
Err. Clear
DeleteFile = False
End If
Call fso. DeleteFile (filespec)
Set fso = Nothing
DeleteFile = True
End function

Public function RenameFile (filespec1, filespec2)
'Modify an object
Dim fso
Set fso = CreateObject ("Scripting. FileSystemObject ")
If Err. number <> 0 Then
Response. Write ("An error occurred while modifying the file name! View error information
"& Err. number &"
"& 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
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.