Using a disconnected data recordset in an ASP program

Source: Internet
Author: User

When we use ASP's built-in ADO component for database programming, we usually open a connection at the beginning of the script and close it at the end of the script, but in most cases the connection is open more often than it needs to open for larger scripts. Therefore, to conserve server resources, the connection should be shut down as much as possible to free up resources for the connection, and the technique of shutting down the recordset without closing the recordset is called a disconnected recordset, which itself is called a disconnected recordset.

Here's an example of how this technique is used (Northwind.mdb is a database from Microsoft Access97, file Adovbs.inc can be C:\Program Files\Common Files\ System\ado found below):

<% @LANGUAGE = VBScript%>
!--#include file= "Adovbs.inc"-->
<%
Response.Expires = 0
Dim Cnn,objrs, Strout, STRQ, STRC
strc= "Driver={microsoft Access Driver (*.mdb)}; Dbq= "& Server.MapPath (" \ASP24 ") &" \northwind.mdb; "
' Establish a connection
Set Cnn = Server.CreateObject ("ADODB.") Connection ")
Cnn.open STRC
' Create a Recordset object
Set objRS = Server.CreateObject ("ADODB. Recordset ")
Objrs.cursorlocation =aduseclient
Objrs.cursortype = adOpenStatic
Objrs.locktype = adLockOptimistic
STRQ = "Select Shippers ID, company name, telephone from shippers"
Objrs.open STRQ, Cnn,,, adCmdText
Set objrs.activeconnection = Nothing ' Disconnect recordset
Cnn.close ' Close connection
Set Cnn = Nothing
Response.Write "<HTML> <BODY>"
' Use a broken recordset below
Do as (not objrs.eof)
Strout = objRS ("Freight forwarder ID") & "," & objRS ("Company name") & "," & objRS ("Phone")
Response.Write Server.HTMLEncode (Strout) & "<BR>"
Objrs.movenext
Loop
Response.Write <BR> Prepare new or inserted records:
' If you need to update the database, 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 Then
Objrs.addnew
objRS ("company name") = "Wu Feng"
objRS ("telephone") = "571-7227298"
Objrs.update
Response.Write "records that qualify for this condition do not exist, new. <BR>"
Else
objRS ("telephone") = "571-7227071"
Response.Write "A record that matches this condition exists, Update. <BR>"
Objrs.update
End If
Set objrs.activeconnection = Nothing
Cnn.close
Set Cnn = Nothing
Objrs.close
Set objRS = Nothing
Response.Write "</BODY> </HTML>"
%>

Related Article

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.