Use the disconnected record set in ASP

xiaoxiao2021-03-06  40

When we use the ASP built-in ADO component to program database programming, we usually open a connection at the beginning of the script, and close it at the script, but in large scripts, in most cases, the connection is more than it. It is much longer than the time you need to open. Therefore, in order to save server resources, the connection should be closed as much as possible to release the resources occupied by the connection, which close the record set without closing the technique of the recordset is called the disconnected record set, this recordset itself is called a disconnected record set.

Below we will explain this technique by an example (northwind.mdb is a database comes with Microsoft Access, file adovbs.inc can be found under C: / Program Files / Common files / system / ado):

<% @Language = VBScript%>

<%

Response.expires = 0

DIM CNN, OBJRS, STROUT, STRQ, STRC

Strc = "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.mappath ("/ asp24")

& "/Northwind.mdb;"

'establish connection

SET CNN = Server.createObject ("AdoDb.Connection")

CNN.Open STRC

'Creating a RecordSet object

Set objrs = server.createObject ("adoDb.recordset")

Objrs.cursorLocation = aduseclient

Objrs.cursortype = adopenStatic

Objrs.lockType = AdlockOptimistic

STRQ = "SELECT shipper ID, company name, phone FROM shipper"

Objrs.open strq, CNN,, AdcmdText

Set objrs.activeConnection = Nothing 'Disconnect record set

CNN.Close 'Close connection

SET CNN = Nothing

Response.write ""

'Use the disconnected record set below

Do While (not objrs.eof)

Strout = Objrs ("Continental ID") & "," & Objrs ("Company Name") & "," & Objrs ("Phone")

Response.write Server.htmlencode (Strout) & ""

Objrs.movenext

Loop

Response.write "

Prepare new or insert records: "

'If you need to update the database, you must re-establish the connection.

SET CNN = Server.createObject ("AdoDb.Connection")

CNN.Open STRC

Set objrs.activeConnection = CNN

Objrs.filter = "Company Name = 'Wu Feng'"

IF objrs.eof kil

Objrs.addnew

ObJRS ("Company Name") = "Wu Feng"

ObJRS ("Phone") = "571-7227298"

Objrs.Updateresponse.write "Added.

"

Else

ObJRS ("Phone") = "571-7227071"

Response.write "Update is existent."

Objrs.update

END IF

Set objrs.activeConnection = Nothing

CNN.Close

SET CNN = Nothing

Objrs.close

Set objrs = Nothing

Response.write ""

%>

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

New Post(0)