Using VB to generate DLL to encapsulate ASP code, connect database

Source: Internet
Author: User
Tags connect reference access database
Encapsulation | Connection database This article connects the database with VB-generated DLLs to encapsulate ASP code (for example, in an Access database).

In general, when we use ASP to connect to an Access database, we typically do the following:

'//proconn.asp
<%
Dim proconn
Set Proconn=server.createobject ("ADODB. CONNECTION ")
Proconn.open "Driver={microsoft Access driver (*.mdb)};uid=;p wd=123;dbq=" & Server.MapPath ("db.asp")
' An Access database that was originally db.mdb changed the suffix to db.asp file, database password 123

If Err. Number <> 0 Then
Response.Write "Database is not connected, please check"
Response.End
Else
Response.Write "Database Link succeeded"
Response.End
End If
%>

If the server is configured to access proconn.asp, the database connection success will be exported if the connected database succeeds.

However, the security level of such ASP code is very low, if this ASP original generation is seen, then have this database file, other people can easily
Open your database and do the operation.

So our mission is here, how do you encapsulate these key things?

First you need to identify ways and means and objects.

Look at some of the information on the web, mainly using VB to generate DLLs to encapsulate the method, then we also take such a good way, (although I have not really used VB this dongdong)

Way to determine, then what is the object we need to encapsulate?

Let's see.
"Driver={microsoft Access Driver (*.mdb)};uid=;p wd=123;dbq=" & Server.MapPath ("db.asp")
Is the most critical code, this code is encapsulated in a VB-generated DLL, should be better.
The reason for not putting the whole
Dim proconn
Set Proconn=server.createobject ("ADODB. CONNECTION ")
Proconn.open "Driver={microsoft Access driver (*.mdb)};uid=;p wd=123;dbq=" & Server.MapPath ("db.asp")
are encapsulated (because there is a talk on the web that encapsulates the entire connection code) because when other ASP files reference proconn.asp,
I also need the inside of the proconn to do other operations, if the encapsulation, inconvenient to reference and operation.
(The above Package object explanation reason is my personal opinion, has the friend to say the overall encapsulation to the Proconn use has no influence, this I do not understand, please know to tell me)


Just want to encapsulate the most critical part "" "Driver={microsoft Access Driver (*.mdb)};uid=;p wd=123;dbq=" & Server.MapPath ("db.asp") "

To analyze the contents of this section to be encapsulated,
The first half of it is a string:
"Driver={microsoft Access Driver (*.mdb)};uid=;p wd=123;dbq="
Another string that connects the second half of the upper part with &.
Another string in the second part is the return value of the Server.MapPath object function.



Here's how to start the encapsulation process.
First of all
Create a new VB under the ActiveX DLL project, the project name Project1 changed into Condbdll?? The name of the method Class1 is changed to CS
The project name and method name will be used when calling this DLL, you can define your own naming rules, but please be careful to use them well.
The code section 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 to simplify the original object and handle it in two basic functions.

Public Function Connectdb () as Variant

Connectdb = "Driver={microsoft Access driver (*.mdb)};uid=;p wd=123;dbq="

End Function
' The above function is to handle the first half of the string, and directly return the contents of the string

' also define the following function to handle the second half of the content
Public Function DBPath () as Variant
DBPath = Sr. MapPath ("db.asp")
End Function
' Note that the SR is used and do not use it as a server

To the critical step, add the Microsoft Active Server Pages ObjectContext Object Library to this project Reference
Add a method, select "Project"-> "Reference" in the menu, and select it in the Open dialog box.
By the way, also select the "Microsoft ActiveX Data Objects 2.6 Library"

Once we've done that, we can compile the build DLL, (don't forget the name of the previous project and method)

Prepare database file Db.asp (by db.mdb change suffix, password 123)

The following is the ASP file code that invokes the encapsulated connection database:
'//proconn.asp
<%
Dim proconn
Set Proconn=server.createobject ("ADODB. CONNECTION ")
Dim condb
Set Condb=server.createobject ("Condbdll.conn")
' Condb is the DLL object created

Dim strconn
' Define a string
strconn = Condb.connectdb () & Condb.dbpath ()
' Connect two parts of the content to form a string
Proconn.open strconn
' Perform database object operations
%>


Since you created the DLL yourself, you must register to use it when you copy it to the appropriate directory.
Registered method, executing in run:
Regsvr32.exe LyfUpload.dll

The method for canceling the registration of this DLL is: regsvr32.exe/u LyfUpload.dll

After registering, our work is basically done, now we can use this encapsulation method to connect with the targeted database.

But one thing that needs special attention is:
Because
Dim condb
Set Condb=server.createobject ("Condbdll.conn")
' Condb is the DLL object created
This is the object created in ASP, including Proconn, so we remember to release the two objects in any other ASP file that uses the (referenced) proconn.asp!
Proconn.close
Set proconn=nothing
Set condb=nothing
Otherwise the system becomes more and more overwhelmed by the fact that the object is not released.

For a method that encapsulates ASP code to connect to an Access database, I want to fully apply the connection method for other databases.

I think my method is not the best, there are any deficiencies, please see the people to correct me, in the first place.




Related Article

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.