Make a compilation component for your ASP program (middle)

Source: Internet
Author: User
The next step is to copy all the code from the VBScript object and paste it into the class window shown by the New Project Wizard. Just copy the code from "Class DbConnection" to "End class." After pasting the code, In the Properties window, change the name of the class to DbConnection, as shown in Figure C.


At the same time, change the MTSTransactionMode to 3 (using the transaction process). We do not need to add any code specially written for this object to support the transaction processing (transaction) process, but it is best to affirm that your object will support transaction processing. Now, Save your projects and classes on your hard drive. I think using these names will reduce confusion, but if your tastes are different from mine, you can change the names of those files.

Because this object uses ADODB, so the next step is to add a reference to that library in the project. Select the "Project" menu and select "Reference ...". The dialog box that appears will display a list of library files available on your computer, as shown in Figure D. Browse this list and select Microsoft ActiveX Data Objects 2.5 Library check box. My system has SQL 2000 installed, but if you don't have the 2.5 version, you can choose the most recent version of your system. This component does not use any of the newly released features, so most active Data The objects version works.

Figure D

Code modification
So far, we have successfully moved the code of the DbConnection class into Visual Basic. For most of the time, this code is complete. In addition to the class's constructor for ASP Application the reference to the object and the Server.CreateObject line, the code can be compiled. In order to ensure a well compiled object, a little bit of code modification is necessary.

First, add the "Option Explicit" line at the top of the code window so that the system generates an error warning when the variables are used before they are declared. Similarly, the dimension of the mconnectionstring is moved to Option Explicit stated below. Then, change the Class_Initialize () process as shown in Figure B. This code simply moves the connection string code directly inside the object. This adds a little security, Because a compiled object can connect to the database without exposing the actual connection string to the developer or unauthorized user.

Listing B
Private Sub Class_Initialize ()
mconnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data source=c:\temp\music Collection.mdb;"
End Sub

To ensure that your component does not expose the connection string, you can remove the Public property getconnectionstring () code from the class. This allows the ConnectionString attribute to become read-only, This allows the user to use this class to access other data sources, but it prevents the property from being read, so the user cannot see the connection string.

You can also remove the class_terminate () process because it does not contain any code. You can use a lookup replacement procedure, "Server.CreateObject" as the lookup text and "CreateObject" Replace them as replacement text. This makes the creation of all objects compatible with Visual Basic. You should save the project by this time. Then, just for testing purposes, select Create DBUtil.dll from the File menu to compile the DLL. This component should be able to compile successfully, It will not work properly without further modification.

The remaining revisions are numerous but simple. Because Visual Basic type checking is more powerful than VBScript, you should type-qualify all functions and variables in the class. This job will not take long to help generate a more efficient code. List C shows the completed classes.


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.