Use VB to generate DLL to encapsulate ASP database connection code

Source: Internet
Author: User

This article uses the DLL encapsulated ASP code generated by VB to connect to the database (using the Access database as an example ).

In general, the following operations are usually performed when we use ASP to connect to the Access database:

'// Proconn. asp
<%
Dim ProConn
Set ProConn = Server. CreateObject ("ADODB. CONNECTION ")
ProConn. Open "driver = {Microsoft Access Driver (*. mdb)}; uid =; pwd = 123; DBQ =" & Server. MapPath ("DB. asp ")
'The Database Password is 123 for an Access database that is originally DB. mdb and is suffixed with DB. asp.

If err. Number <> 0 then
ResPonse. Write "the database is not linked. 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 is connected successfully, "database connection succeeded" is output ".

However, the security level of such asp code is very low. If the original asp code is seen by others, then other users can easily access this database file.
Open your database as needed.

So our task is coming. How can we encapsulate these key contents?

First, you must determine the method and object.

I checked some information on the Internet, which is mainly encapsulated by using VB to generate DLL, so we can also adopt this method (although I haven't actually used VB)

Determine the method and method, so what are the objects we need to encapsulate?

Let's see
"Driver = {Microsoft Access Driver (*. mdb)}; uid =; pwd = 123; DBQ =" & Server. MapPath ("DB. asp ")
Is the most critical code. This code is encapsulated in a DLL generated using VB and should be better.
The reason why the entire
Dim ProConn
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 the entire connection code is encapsulated on the Internet) because when Proconn. asp is referenced by other asp files,
I also need ProConn to perform other operations. If it is encapsulated, it is inconvenient to reference and operate.
(The reason for the above encapsulation object is my personal opinion. Some friends said that the overall Encapsulation has no effect on the use of ProConn. I don't understand this. Please tell me if you know it)

Only encapsulate the most critical part "" driver = {Microsoft Access Driver (*. mdb)}; uid =; pwd = 123; DBQ = "& Server. mapPath ("DB. asp ")"

Analyze the content to be encapsulated,
The first half is a string:
"Driver = {Microsoft Access Driver (*. mdb)}; uid =; pwd = 123; DBQ ="
Connect the second half of the string.
Another string in the second half is the return value of the Server. MapPath object function.

Next we will start the encapsulation operation process.
First
Create an ActiveX DLL project under VB, and change project name Project1 to ConDBDLL ?? Method class1 name changed to cs
The project name and method name will be used when calling this DLL. You can define them according to your naming rules, but please be careful when using them.
The DLL code 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 required. The original object is 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 returns the content of the string directly.

'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 sr is used above. Do not use Server.

To the key step, add the Reference "Microsoft Active Server Pages ObjectContext Object Library" to this project
Add method, select "Project"-> "Reference" from the menu, and select in the displayed dialog box.
By the way, select "microsoft activeX data objects 2.6 library"

After completing the above operations, we can compile and generate the DLL. (do not forget to change the project name and method name)

Prepare the database file DB. asp (changed by DB. mdb suffix, password 123)

The following is the asp file code that calls the encapsulated database connection:
'// ProConn. asp
<%
Dim ProConn
Set ProConn = Server. CreateObject ("ADODB. CONNECTION ")
Dim ConDB
Set ConDB = Server. CreateObject ("ConDBDLL. Conn ")
'Condb is the created DLL object.

Dim StrConn
'Define a string
StrConn = ConDB. ConnectDB () & ConDB. DBPath ()
'Concatenate two parts to form a string
ProConn. Open StrConn
'Perform database object operations
%>

Because it is a self-created DLL, after copying it to the corresponding directory, it must be registered before it can be used.
The registration method is executed in "run:
Regsvr32.exe LuanLuanDBCONN. dll

The method to cancel the registration of this DLL is: Regsvr32.exe/u LuanLuanDBCONN. dll

After registration, we basically finished our work. Now we can use this encapsulation method to connect to a targeted database.

However, note the following:
Because
Dim ConDB
Set ConDB = Server. CreateObject ("ConDBDLL. Conn ")
'Condb is the created DLL object.
This is the object created in ASP, including ProConn, So we remember to release these two objects in any other asp files that use (reference) ProConn. ASP!
ProConn. close
Set ProConn = nothing
Set ConDB = Nothing
Otherwise, the system will become increasingly overwhelmed because the objects are not released.

For how to encapsulate the ASP code to connect to the Access database, I would like to apply the connection method of other databases.

Encapsulate other database connections.

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.