This article uses the DLL generated by VB to encapsulate the ASP code to connect to the database (taking the Access database as an example).
Under normal circumstances, when we use ASP to connect to the Access database, we usually perform the following operations
'//Proconn.asp
<%
dimProConn
set ProConn=Server.CreateObject("ADODB.CONNECTION")
ProConn.Open "driver={Microsoft Access Driver (*.mdb)};uid=;pwd=123;DBQ=" & Server.MapPath("DB.asp")
'An Access database that was originally DB.mdb is changed to a file with a suffix of DB.asp, and the database password is 123
if err.Number <> 0 then
ResPonse.Write "There is no link to the database, please check"
ResPonse.End
else
ResPonse.Write "Database connection successful"
ResPonse.End
end if
%>
If the server is configured, access Proconn.asp. If the database connection is successful, "Database connection successful" will be output.
However, the security level of such asp code is very low. If the original asp is seen by others, then if there is this database file, others can easily open your database for operations.
So here comes our task, how to encapsulate these key contents?
First, you need to determine the method, method and object.
After checking some information on the Internet, it is mainly encapsulated by using VB to generate DLL, so we should also adopt this method, (although I have not really used VB)
method to determine, so what Is it the object we need to encapsulate?
Come and see everyone
"driver={Microsoft Access Driver (*.mdb)};uid=;pwd=123;DBQ=" & Server.MapPath("DB.asp")
It is the most critical code. It should be better to encapsulate this code in a DLL generated with VB.
The reason why not the whole
dimProConn
set ProConn=Server.CreateObject("ADODB.CONNECTION")
ProConn.Open "driver={Microsoft Access Driver (*.mdb)};uid=;pwd=123;DBQ=" & Server.MapPath("DB.asp")
All are encapsulated (because there are instructions on the Internet to encapsulate the entire connection code) because when other asp files reference Proconn.asp,
I also need the ProConn inside to perform other operations. If it is encapsulated, it will be inconvenient to reference and operate.
(The above explanation of the encapsulation object is my personal opinion. Some friends said that the overall encapsulation has no impact on the use of ProConn. I don’t understand this. Please tell me if you know)
I just want to encapsulate the most critical part ""driver={Microsoft Access Driver (*.mdb)};uid=;pwd=123;DBQ=" & Server.MapPath("DB.asp")"
to analyze this paragraph. the contents of the package,
The first half of it is a string:
"driver={Microsoft Access Driver (*.mdb)};uid=;pwd=123;DBQ="
Use & to concatenate the second half of another string.
The other string in the second half is the return value of the Server.MapPath object function.
Let's start the encapsulation operation process.
First, create a new ActiveX DLL project under VB. Change the name of the project Project1 to ConDBDLL?? Change the name of method class1 to cs
The project name and method name will be used when calling this DLL. You can define them according to your own naming rules, but please be careful to use them carefully.
The code part of this DLL is written as follows:
Dim rp As Response
Dim rq As Request
Dim ap As Application
Dim sr As Server
Dim sn As Session
Public Sub OnStartPage(MyScriptingContext As ScriptingContext)
Set rp = MyScriptingContext.Response
Set rq = MyScriptingContext.Request
Set sr = MyScriptingContext.Server
Set ap = MyScriptingContext.Application
Set sn = MyScriptingContext.Session
End Sub
Public Sub OnEndPage()
Set rp = Nothing
Set rq = Nothing
Set sr = Nothing
Set ap=Nothing
Set sn=Nothing
End Sub
'The above statement is necessary. The original object has been simplified and processed in two basic functions.
Public Function ConnectDB() As Variant
ConnectDB = "driver={Microsoft Access Driver (*.mdb)};uid =;pwd=123;DBQ="
End Function
'The above function processes the first half of the string and directly returns the content of this string
' In addition, define the following function to process the second half of the content
Public Function DBPath() As Variant
DBPath = sr.MapPath("DB.asp")
End Function
'Note that the above uses sr, do not use it as Server. Now we
have reached the critical step. Add the Reference of "Microsoft Active Server Pages ObjectContext Object Library" to this project.
To add a method, select "Project"->"Reference" in the menu and select it in the opened dialog box.
By the way, we also need to select "microsoft activeX data objects 2.6 library"
to perform the above operations. We can compile and generate DLL. (Don't forget to change the project name and method name earlier.)
Prepare the database file DB.asp (written by DB .mdb is formed by changing the suffix, password is 123)
The following is the code for calling the encapsulated asp file that connects to the database:
'//ProConn.asp
<%
dimProConn
set ProConn=Server.CreateObject("ADODB.CONNECTION")
DimConDB
set ConDB=Server.CreateObject("ConDBDLL.Conn")
'ConDB is the created DLL object
Dim StrConn
'Define a string
StrConn = ConDB.ConnectDB() & ConDB.DBPath()
'Concatenate the two parts to form a string
ProConn.OpenStrConn
'Perform database object operations
%>
Since it is a DLL created by yourself, after copying it to the corresponding directory, it must be registered before it can be used.
Registered method, executed in "Run":
Regsvr32.exe lyfUpload.dll
The method to cancel the registration of this DLL is: Regsvr32.exe /u lyfUpload.dll
After the registration is completed, our work is basically done. Now we can use such an encapsulation method to connect to a targeted database. .
However, there is one thing that needs special attention:
because
DimConDB
set ConDB=Server.CreateObject("ConDBDLL.Conn")
'ConDB is the created DLL object. This is an object created in ASP, including ProConn. Then we remember to release these two objects in any other ASP files that use (reference) ProConn.asp!
ProConn.close
setProConn=nothing
setConDB=Nothing
Otherwise, the system will become increasingly overwhelmed because objects are not released.
Regarding this method of encapsulating ASP code to connect to the Access database, I think it is completely applicable to the connection method of other databases.
I think my method is not the best. If there are any shortcomings, please correct me if you have read it. Thank you in advance.