Writing components with VB - Connection string of packaging database

zhaozj2021-02-16  56

Use VB6 writing components to hide the connection string of the database

Hainan Provincial Commercial Information Service Center Wen Wen

Today, most of the development of web applications use browser / server mode, and in the field of B / S application development, Microsoft's IIS / ASP combination is in its powerful features, good extension capabilities and other Microsoft products. Good compatibility is rapidly popular. The ASP is aware of its simple and easy to learn, powerful, and most of the domestic websites use ASP architecture. When we use the ADO to access the database, sometimes it is explicitly written in the .asp file, which is obviously less secure, it is easy to get a password, database name and other information. For the security of data, we can write components to encapsulate the string of the database, and then call on the global.asa file or .asp.

First, let's take a step in step by step:

Start VB6.0 New -> ActiveX DLL Project. Click "Project" -> reference, select "Microsoft Active Server Pages Object Library" and "Microsoft ActiveX Data Objects 2.1 Library". Change the name of the class module to WenConnection. Change the name of the project to WenadoDB. Save Project File WenadoDB.vbp and Class File WenConnection.CLS. Details: 1) Select "Project" -> "Reference" Enter the reference User Select Interface As shown in Figure 1, in the "Available reference" checkbox Select "Microsoft Active Server Pages Object Library" and "Microsoft ActiveX Data Objects 2.1 Library ".

figure 1

2) Select "Project" -> "Engineering Properties" to enter the engineering property setting interface, select "General" page, select "ActiveX DLL" in the "Engineering Type" drop-down box, enter the project name in Engineering Name input box " WenadoDB, as shown in Figure 2.

figure 2

3) Select "Compile" page, select "Code Size Optimization", as shown in Figure 3.

image 3

At this point, we have completed the basic settings such as the attributes, references such as new projects.

Second, let's write the code in class wenconnection.cls:

1) First of all, the variable is required:

Private WenscriptingContext As ScriptingContext

Private WenApplication AS Application

Private WenRequest As Request

Private WenResponse As Response

Private WenServer AS Server

Private Wensession Assession

2) In order to use the ASP built-in object in the WenConnection class, you must write an onStartPage subfunction in this class. That is because when the user accesses an ASP file with this component, IIS will use ScriptingContext to our object. Please use. This ScriptingContext includes all ASP methods and properties, which makes we have the ability to access all ASP objects.

Public Sub onstartPage (PassedscriptingContext as scriptingcontext)

SET WenscriptingContext = PassedscriptingContext

SET WenApplication = WenscriptingContext.Application

SET WENREQUEST = WenscriptingContext.RequestSet WenResponse = WenscriptingContext.Response

SET WENSERVER = WenscriptingContext.server

SET WENSESSION = WenscriptingContext.Session

End Sub

Since we use the onStartPage function to create an object, then we use the OneNDPage subunies here to release the object:

Public Sub OneendPage ()

Set wenscriptingContext = Nothing

SET WenApplication = Nothing

SET WENREQUEST = Nothing

SET WENRESPONSE = Nothing

SET WENSERVER = Nothing

SET WENSESSION = Nothing

End Sub

Next define two functions rsResult () and DataSource ():

Public Function RS (STRSQL AS String) As Recordset

DIM OCONN AS Connection

DIM ORS As Recordset

DIM STRCONNSTRING AS STRING

StrConnstring = "driver = {sql server}; server = servername; uid = sa; pwd =;" & _

"Database = DatabaseName"

Oconn.open straconnstring

Ors.activeConnection = OCONN

strsql = "SELECT * from TableName"

Ors.open strsql, Oconn, 1, 3

SET RS = ORS

END FUNCTION

Public Function DataSourceConnection () As Variant

DataSourceConnection = "driver = {SQL Server}; server = servername; uid = sa; pwd =; database = databasename"

END FUNCTION

Third, the deployment project is WenadoDB.vbp and saves class named WenConnection.CLS, then click "File" -> "Generate WenadodB.dll" to compile into dynamic connection library files. VB also registers the component in the registration table while compiling the component, if you want to register the component on another machine, use the following instructions to register or register:

Regsvr32 X: / Path / Wenadodb.dll x: / Path / Path and path stored for WenadoDb.dll files

Regsvr32 / u x: / Path / Wenadodb.dll parameter u is anti-registration

Fourth, call the WenadoDB.dll component in the ASP file.

<%

Set conn = server.createObject ("Wenadodb.wenConnection") 'Call Components Create Object Examples

Objconn = conn.datasourceConnection ()

Application ("strconn") = Objconn

SET RS = Server.createObject ("AdoDb.Recordset")

SQL = "Select * from tablename ORDER BY ID DESC" RS.Open SQL, Application ("StrConn"), 1, 3

%>

<%

IF rbof and rs.eof the

"" There is no data yet. "

Else

Do While Not Rs.eof

%>

<% = rs ("Field1")%> <% = RS ("Field2")%>

<%

Rs.movenext

Loop

END IF

Rs.close; set = Nothing

%>

Five, small knot

We only write a simple dynamic connection library file for connecting the database. Using VB's powerful component writing feature, you can also write a more powerful comprehensible component to complete more practical tasks.

转载请注明原文地址:https://www.9cbs.com/read-23242.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.038, SQL: 9