Basic ADO and SQL Tutorial
page 4 of 5
by Steven Smith
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 47983/ 81

Recordset Object

The Recordset object is the primary object you will work with when you are accessing data using ASP and ADO. A Recordset is essentially a database cursor, which you can page through to view each row of a query or table. To begin with, we'll declare our recordset:

 'Create Recordset Object
 Dim objRst   'Create Recordset Variable
 Set objRst = Server.CreateObject("ADODB.Recordset")
 Set objRst.ActiveConnection = objConnect
 Set objRst.Source = objCmd

Note that our Recordset references our connection objConnect and our Command object, objCmd. This is the minimal requirements for a recordset that uses the command object. You can get away with fewer parameters when declaring your recordset, if you opt not to use the Command object(in fact there are a few different ways to set up your recordset), but I won't cover them here. You should understand two additional parameters for your recordset which can affect how your query performs. They are the CursorType and LockType properties. Each of these has 4 possible values. The default values give you a Read-Only, Forward-Only recordset. For the operations we will be performing in this tutorial, the default options are ideal. If you choose to use the Recordset object instead of the Command object for your updates and inserts, you will need to become more familiar with the Cursor and Lock types. Here is an excerpt from the file, listing the cursor and locktypes:

 '---- CursorTypeEnum Values ----
 Const adOpenForwardOnly = 0
 Const adOpenKeyset = 1
 Const adOpenDynamic = 2
 Const adOpenStatic = 3

 '---- LockTypeEnum Values ----
 Const adLockReadOnly = 1
 Const adLockPessimistic = 2
 Const adLockOptimistic = 3
 Const adLockBatchOptimistic = 4

Note that Cursor is 0 to 3 and Lock is 1 to 4. Don't ask me why Microsoft did it this way. To set these properties, you would use code like:

 'Set Locktype and Cursortype
 Set objRst.Locktype = adLockReadOnly
 Set objRst.Cursortype = adOpenForwardOnly

Now that we've managed to declare all of our necessary objects, we're finally ready to access some data. The first thing you need to do is test out an SQL query to make sure it returns some results from your database. A simple "SELECT * FROM TableName" will suffice. Use your query for the CommandText in the following piece of code, with a valid column name in place of "field":

 objCmd.CommandText = "SELECT * " & _
  " FROM mytable " 
 If Not objRst.EOF Then
 End If

When you execute this SQL query, it will print the first record's "field" value. If no values exist, it will do nothing. If you do not test for EOF before attempting to access recordset values, you will generate a run-time error (when you reach EOF). Always test for EOF before attempting to access your recordset. Note that I immediately close the recordset. Get in the habit of closing your recordsets as soon as you are finished using them, especially within loops. If you attempt to open a recordset that is already open, you will generate a run-time error.

Update: If you loop through a recordset, make sure you call objRst.MoveNext within your While Not objRst.EOF loop.  Otherwise you'll enter an infinite loop.  This is an extremely common mistake.  Also, avoid looping through recordsets.  A much faster technique is to use GetRows, which will return the results in an array.  Here's some more info on GetRows.

So far we haven't done anything that required the use of the command object, although it is a nice debug tool to be able to write out the CommandText property to check your SQL. The next example demonstrates why the Command object works better than the ADO recordset object for manipulating the data in your database.

 objCmd.CommandText = "UPDATE mytable SET " & _
  " field = 'Somevalue'" & _
  " WHERE field = 'Some Other Value' "

Note that for this Command, you do not need to close your recordset. This command uses your database's own UPDATE routine, which will almost always be far faster than using ASP's ADO Recordset object to perform your update. Ideally, you should move all critical database calls to stored procedures on your database server, but the above method is almost as efficient.  Similarly, you can perform the following statements using your database:

 objCmd.CommandText = "DELETE FROM mytable " & _
  " WHERE field = 'Some Other Value' "
 objCmd.CommandText = "INSERT INTO mytable " & _
  " (field1,field2) " & _
  " VALUES ("this value","this other value") "

Note once again that I didn't have to close the recordsets between operations, because they do not result in any data being sent to the recordset. You only need to worry about closing your recordset/command object combination when you are performing SELECT statements.

Update: You really really really should use stored procedures for your database access, for security reasons as well as performance.  Using SQL in your page comprised of strings concatenated together opens up your code to SQL Injection Attacks.

View Entire Article

User Comments

Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:29:43 PM
[/pre]Cheap NFL,NBA,MLB,NHL
[url=]Jerseys From China[/url]
[url=]2012 nike nfl Jerseys[/url]
[url=]cheap China Jerseys[/url]
[url=]Sports Jerseys China[/url]
[url=]NFL Jerseys China[/url]
[url=]NBA Jerseys China[/url]
NHL Jerseys China
[url=]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[pre]We Are Professional China jerseys Wholesaler
[url=]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url=]2012 mlb all atar jerseys[/url]
[url= [/url]Cheap China Wholesael[/url]
[url=]Wholesale jerseys From China[/url]
[url=]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
We are professional jerseys manufacturer from china,wholesal
sports [url=]Jerseys From China[/url]
[url=]NFL jerseys China[/url]
[url=]NHL Jerseys China[/url]
[url=]NBA Jerseys China[/url]
[url=]MLB Jerseys China[/url]
[url=]China Jerseys[/url],Free Shipping
We are professional jerseys manufacturer from china,wholesal
sports [url=]cheap jerseys sale online [/url]
[url=]2012 nike nfl Jerseys[/url]
[url=]cheap NFL jerseys China[/url]
[url=]NHL Jerseys C
Title: Classic asp rocks always   
Name: HariFinhead
Date: 2011-01-10 2:12:29 AM
Classic asp always rocks in many manufacturing company still now.. even .net has came..
Title: mike   
Date: 2010-12-22 6:34:55 AM
"I strongly recommend you jump to ASP.NET instead."

Honestly, dot net blows. ASP is so much nicer.
Title: Much Easier Thank I Thought   
Name: SQLTutorials
Date: 2010-11-10 11:08:39 AM
Much Easier Thank I Thought. Thanks a lot.
Title: Other self hosting solutions   
Name: mer
Date: 2010-05-10 1:35:23 PM
There better solutions than Cassini if you need to host your ASP.NET apps without IIS. Cassini has some big drawbacks - doesn't allow connections to WCF except for localhost, security issues surrounding the account that the process must run under, and most importantly lack of SSL and logging features.

Other solutions: - a commercial embeddable ASP.NET web server with SSL and logging - a commercial version of Cassini with an installer - another fork of the cassini source
Title: Hi   
Name: Prashanth
Date: 2009-04-06 8:16:46 AM
It was very useful for giving some good information on ADO for the beginners
Title: nbp   
Name: www
Date: 2008-05-17 3:32:07 PM
query of searching data in sqlserver with use of
Title: swd   
Name: www
Date: 2008-04-29 6:06:09 AM
Title: Hi...   
Name: Hann
Date: 2007-03-20 9:49:00 AM
Can U tell me how to connect MDB file in other host..?

Plizz, Send your reply to

Title: hi   
Name: Paapu
Date: 2007-01-23 1:51:19 AM
Its very useful, but still u can add some details also..
Title: hll   
Name: pat
Date: 2006-11-17 5:45:08 AM
hello thanks a bunch but i guess i need more help
Title: DotNetSpace   
Name: Simple way to handle exception in database connections
Date: 2006-10-26 1:59:35 PM
This is a simple way to handle exceptions when using database queries. This link includes an example:
Title: DotNetSpace   
Name: DotNetSpace query database connection example
Date: 2006-10-26 1:58:44 PM
Shows a simple way to handle exceptions when using database queries. This link includes the example.
Title: use of loks in asp   
Name: praveen
Date: 2006-07-11 9:56:53 AM

Please provide me the information about locks.
Title: asp stuff   
Name: dhvanil
Date: 2006-04-06 10:12:35 AM
your site is good
put some examples of every methods
Title: webmaster   
Name: Yusef Jeffries-El
Date: 2006-01-15 1:10:43 AM
I just needed a quick reference to the syntax of the INSERT INTO statement using Visual Basic
Title: hi   
Name: vishal
Date: 2005-06-03 10:17:27 AM
vishal patel your site is so genious but add the download button on every page

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-22 7:47:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search