Writing components with VB-encapsulating the connection string for a database

Source: Internet
Author: User
Tags iis
Packaging | data | database | Strings Nowadays, most of the development Web applications are using the Browser/server mode, while in the B/s Application development field, Microsoft's iis/asp combination has the powerful function, the good expansion ability and the good compatibility with other Microsoft products, Pop up quickly. ASP with its simple and easy to learn, powerful and win the love of the vast number of programmers, most of the domestic web sites are the use of ASP architecture. When we use ADO to access a database, we sometimes write the connection string explicitly in the. ASP file, this is obviously not very safe, it is easy to be a person with ulterior motives to obtain passwords, database names and other information. For data security, we can write our own component to encapsulate the string that accesses the database, and then call it on the Global.asa file or. asp.

First, let's take a step-by-step to create a component:

Start vb6.0 new-->activex DLL project. Click the Project--> Reference and select both 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 the project file Wenadodb.vbp and the class file WENCONNECTION.CLS. Specific approach: 1 Select "Project"-> "Reference" to enter the reference user selection interface as shown in Figure 1, in the Available References check box, select Microsoft Active Server Pages Object library and Microsoft ActiveX Data Objects 2.1 Library "two items.


Figure 1

2 Select "Engineering"-> "Engineering properties" into the Project property settings interface, select the "General" page, in the "Project type" drop-down box, choose "ActiveX DLL", in the Project name input box, enter the project name "Wenadodb", as shown in Figure 2.


Figure 2

3) and select the "Compile" page, select "Code size Optimization", as shown in Figure 3.


Figure 3

So far, we have completed the basic settings of the new project's properties, references, and so on.

Second, we'll write code in the class Wenconnection.cls:

1 The first thing to declare variables:

Private Wenscriptingcontext as ScriptingContext

Private Wenapplication as Application

Private Wenrequest as Request

Private Wenresponse as Response

Private Wenserver as Server

Private Wensession as session

2 in order to use an ASP's built-in object in the Wenconnection class, you must write a onstartpage child function in this class. That's because whenever a user accesses an ASP file with this component, IIS sends the ScriptingContext to our object and invites us to use it. This scriptingcontext includes all ASP methods and attributes, which gives us the ability to access all ASP objects.

Public Sub OnStartPage (Passedscriptingcontext as ScriptingContext)

Set Wenscriptingcontext = Passedscriptingcontext

Set wenapplication = wenscriptingcontext.application

Set wenrequest = wenscriptingcontext.request

Set Wenresponse = Wenscriptingcontext.response

Set Wenserver = Wenscriptingcontext.server

Set wensession = wenscriptingcontext.session

End Sub

Now that we use the OnStartPage function to create the object, we use the OnEndPage function to release the object:

Public Sub OnEndPage ()

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 strconnstring

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=;d atabase=databasename"

End Function

Third, save the project name is Wenadodb.vbp and save class name is Wenconnection.cls, then click "File"-> "Generate WenADODB.DLL" compile into Dynamic Connection library file. VB in the compilation of dynamic connection library files are also registered in the registration table, if you want to register the component on another machine, please use the following instructions to register or reverse registration:

Regsvr32 x:\ path \wenadodb.dll x:\ path \ disk character and path for WenADODB.dll file

regsvr32/u x:\ Path \wenadodb.dll parameter U for anti-registration

The example of invoking the WenADODB.dll component in the ASP file.

<%

Set conn=server.createobject ("wenadodb.wenconnection") ' invokes the component to create an object instance

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

%>

<table align= "Center" border= "1" >

<%

If Rs.bof and rs.eof then

Response.Write "There is no data for the moment. "

Else

Do as not rs.eof

%>

<tr width=100%>

&LT;TD width=50%><%=rs ("Field1")%></td><td width=50%><%=rs ("Field2")%></td>

</tr>

<%

Rs.movenext

Loop

End If

Rs.close; Set rs=nothing

%>

</Table>

V. Summary

We're just knitting.



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.