Use a disconnected record set in ASP

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 for large scripts, in most cases, the connection is opened much longer than it needs. Therefore, to save server resources, you should close the connection whenever possible to release the resources occupied by the connection. This technology that closes the connection of the record set without closing the record set is called the disconnection record set, this record set is called a disconnected record set.
The following example shows how to use this technology (NorthWind. mdb is a database that comes with Microsoft Access. The file adovbs. inc can be found in C: Program FilesCommon FilesSystemADO ):
<% @ LANGUAGE = VBScript %>
<%
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 forwarder ID, company name, phone number FROM forwarder"
ObjRS. Open strQ, Cnn, ad1_text
Set objRS. ActiveConnection = Nothing 'disconnects the record Set.
Cnn. Close 'Close the connection
Set Cnn = Nothing
Response. Write ""
'Use the disconnected record set below
Do While (Not objRS. EOF)
StrOut = objRS ("freight dealer ID") & "," & objRS ("Company Name") & "," & objRS ("phone ")
Response. Write Server. HTMLEncode (strOut )&""
ObjRS. MoveNext
Loop
Response. Write"
Prepare to add or insert a record :"
'If you want to update the database, you need to reestablish 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 ("phone") = "571-7227298"
ObjRS. Update
Response. Write "if the record that meets this condition does not exist, it is added.
"
Else
ObjRS ("phone") = "571-7227071"
Response. Write "if the record meets this condition exists, Update ."
ObjRS. Update
End If
Set objRS. ActiveConnection = Nothing
Cnn. close
Set Cnn = Nothing
ObjRS. Close
Set objRS = Nothing
Response. Write ""
%>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.