ASP Components Intermediate Getting Started and Professional Series

xiaoxiao2021-03-06  47

Start the database operation.

Common components package

1. • Package the connection information of the database.

1> ???? Directly return to the database connection string, such as the component

PUBLIC? FUNCTION? DATASOURCE ()? AS? VARIANT

DataSource? =? "driver = {SQL? Server}; server = yang; uid = sa; pwd = ;? Database = northwind"

END? FUNCTION

?

ASP call

Set obj = server.createObject ("webdb.getinfo")??

Oconn = obj.datasource ()

Such a disadvantage is clear that in the ASP file, directly response.write Oconn can display the database connection string and does not play an expected role.

?

2> ???? Return to AdoDb.Connection object

Public function getConn () as adodb.connection

SET CONN = New Adodb.Connection

Conn.connectionstring = "provider = sqloledb.1; persist security info = false; user ID = sa; password =; initial catalog = northwind; data source = yang"

Conn.open

Set getConn = conn

END FUNCTION

?

ASP call

Dim DataQuery

Set DataQuery = Server.createObject ("Webdbtest.getInFomation")

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

?

SQL = "SELECT * from Employees"

Rs.Open SQL, DataQuery.getConn, 1, 3???

?

Response.write RS ("Lastname")

Response.write dataQuery.getconn.connectionstring

SET RS = Nothing

?

This looks good, just response.write dataQuery.getconn.connectionstring still displays a database connection string, you can test.

?

?

?

2. • Package the component to record set

You can take a look at http://blog.9cbs.net/online/archive/2003/12/11/7764.aspx

One thing in this code is that the database is connected to the page, and after the connection is successful, the data is started.

<%

Flag = DataQuery.getConn ()

IF flag = false kil

? Response.write "Database is not connected, please check"

? Response.end

END IF

SET RS = DataQuery.geTemPloyeeList ()

......

%>

?

?

Personally think that the best practice is:

Encapsulate the database, the component method is connected to the database, after operation, close

Try to generate an HTML code in the component to do all packages. As of the following way

<%

The 'List method contains the generation of the page, the connection processing, paging processing, etc. ??? DIM Objloop

??? set objloop = server.createObject ("WebDbtest.getInfomation")

?? objloop.list ()

??? set objloop = Nothing

%>

Instead of being partial package.

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

New Post(0)