Click here to Skip to main content
6,920,685 members and growing! (19,966 online)
Email Password   helpLost your password?
Database » Database » Data Access     Intermediate

Simulating Recordsets with ADO.NET

By Alberto Venditti

A proposed class to simulate the ADODB.Recordset behavior in .NET.
VB, SQL, Windows, .NET1.0, .NET1.1, ADO, ADO.NET, SQL2000, VS.NET2003, DBA, Dev
Posted:2 Oct 2004
Views:108,689
Bookmarked:31 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.58 Rating: 4.45 out of 5
2 votes, 11.1%
1

2
2 votes, 11.1%
3
1 vote, 5.6%
4
13 votes, 72.2%
5

Introduction

As any .NET developer knows, the ADO.NET approach to data access is substantially different from the ADODB predecessor. First of all, because it is disconnected and mostly based on the DataSet concept (that involves a client-side data caching), while ADODB was normally used as a connected data access paradigm (with the exception of the so-called "disconnected recordsets"). The only way to use ADO.NET in a connected fashion is using objects like DataReader, Command, and Transaction, that are not so comfortable if you need to scroll a result set making updates to some data based on a row-oriented logic. This was a very common task when working with ADODB, and a lot of programmers coming from a Visual Studio 6.0 experience will miss the Recordset concept: being oriented to disconnected scenarios, ADO.NET currently doesn't support features like server-side cursors, and so it doesn't expose objects similar to the ADODB.Recordset that was very useful to implement row-based logics. Anyone prevents you from continuing to use the ADODB objects while programming on .NET, but if you want to avoid the COM interoperability overhead, this is not the right way.

In this article, I propose a class that simulates the behavior of an ADODB.Recordset on a Microsoft SQL Server 2000 database through the use of ADO.NET "connected objects" (Connection, Command, DataReader,...) and of server-side cursors directly implemented in T-SQL. The proposed class is developed for SQL Server 2000, but can be easily modified to work with other RDBMSs.

How the code works

The class I wrote is named Recordset and it tries to simulate the ADODB.Recordset in its main functionalities. Then, it exposes methods like Open(), Close(), MoveNext(), MovePrevious(), MoveFirst(), MoveLast(), Update() and so on (even if it doesn't currently expose an AddNew() method). To support navigation and random access to rows of a result set without caching data on the client, you need to use a scrollable server-side cursor; this cursor has to be and remain open for all the duration of the connected updates. That's why, behind the scenes of the Recordset.Open() method, a connection is open and a T-SQL cursor is created, based on a given SELECT expression:

cnn = New SqlConnection(mConnectionString)
cmd = cnn.CreateCommand()
cnn.Open()
...
cmd.CommandText = "DECLARE crsr SCROLL CURSOR FOR " & mSelectString
cmd.ExecuteNonQuery()
cmd.CommandText = "OPEN crsr"
cmd.ExecuteNonQuery()

The various movements inside the Recordset have their counterparts in the server-side T-SQL cursor, so it's not difficult to implement for the Recordset class the following methods:

Method T-SQL equivalent
MoveNext() FETCH NEXT FROM crsr
MovePrevious() FETCH PRIOR FROM crsr
MoveFirst() FETCH FIRST FROM crsr
MoveLast() FETCH LAST FROM crsr
MoveAbsolute(n) FETCH ABSOLUTE n FROM crsr
MoveRelative(n) FETCH RELATIVE n FROM crsr

For the Recordset.Update() method, if we suppose the cursor being based on a single-table SELECT statement, we can think to code it as a T-SQL statement like the following:

UPDATE table_name
SET field1=value1, field2=value2,...
WHERE CURRENT OF crsr

In the same way (under the same single-table SELECT statement restriction), also the Recordset.Delete() method can be coded as:

DELETE table_name WHERE CURRENT OF crsr

Finally, the Recordset.Close() method has simply to execute some cleanup code (on the server-side cursor and on the open connection):

cmd.CommandText = "CLOSE crsr"
cmd.ExecuteNonQuery()
cmd.CommandText = "DEALLOCATE crsr"
cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
cnn.Dispose()

The sample application

A sample application has been written to show how to use the Recordset class.

It connects to the Authors table of the famous database Pubs on the local SQL Server (if you want to use another SQL Server or you don't use the deprecated "blank" password for sa, please modify the value associated to the ConnectionString key in the App.config configuration file). The user interface of the sample application is self-explaining: each button simply tests the corresponding method of the Recordset class.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alberto Venditti


Member
I was born in 1970.

I studied Electronic Engineering (graduated in 1997).

Subsequently, I passed some Microsoft exams, and currently I'm certified as:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.
Occupation: Web Developer
Location: Italy Italy

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 28 (Total in Forum: 28) (Refresh)FirstPrevNext
GeneralReg Connection Problem Pinmemberprabakarank20:03 25 Aug '09  
GeneralRe: Reg Connection Problem PinmemberAlberto Venditti23:34 30 Aug '09  
GeneralPlease answer me for a Error Pinmemberphowarso4:09 12 Sep '07  
GeneralRe: Please answer me for a Error PinmemberAlberto Venditti6:51 12 Sep '07  
GeneralHow to Concatinate Two Recordsets in Visual C++6 Pinmemberalisolution20:53 11 May '07  
GeneralRe: How to Concatinate Two Recordsets in Visual C++6 PinmemberAlberto Venditti1:33 14 May '07  
QuestionAccessing mdb files Pinmembershekiman21:48 27 Nov '06  
AnswerRe: Accessing mdb files PinmemberAlberto Venditti22:37 27 Nov '06  
GeneralAccesing MS ACCESS in remote machine Pinmemberhariram287:46 20 Sep '06  
GeneralRe: Accesing MS ACCESS in remote machine PinmemberAlberto Venditti4:25 21 Sep '06  
GeneralRe: Accesing MS ACCESS in remote machine Pinmemberhariram288:25 21 Sep '06  
GeneralRe: Accesing MS ACCESS in remote machine PinmemberAlberto Venditti5:59 22 Sep '06  
QuestionAddnew( ) method Pinmemberpgkdave4:56 29 Aug '06  
I'm new to VB.net and this code is very interesting for reading and updating a record set but has anyone added a Addnew method to the recordset class so that you can add a new record to the table?
GeneralMaking the sample work PinmemberJeppe Andreasen23:09 2 Jun '05  
GeneralRe: Making the sample work PinsussAnonymous9:16 5 Jun '05  
GeneralRe: Making the sample work PinsussAnonymous20:26 5 Jun '05  
GeneralHow make please help me Pinmemberakorolev103:43 20 Oct '04  
GeneralThis is fun and all, but is it advisable PinsussEric the Half-a-Bee3:31 12 Oct '04  
GeneralRe: This is fun and all, but is it advisable PinmemberAlberto Venditti7:18 12 Oct '04  
GeneralRe: This is fun and all, but is it advisable PinsussAnonymous21:14 12 Oct '04  
GeneralServer-side cursors give poor scalability PinmemberPete Appleton23:30 10 Oct '04  
GeneralRe: Server-side cursors give poor scalability PinmemberAlberto Venditti7:15 12 Oct '04  
GeneralRe: Server-side cursors give poor scalability PinmemberPete Appleton7:34 12 Oct '04  
GeneralRe: Server-side cursors give poor scalability PinsupporterNathan Allan20:40 7 Apr '06  
GeneralRe: Server-side cursors give poor scalability PinmemberBlackTigerAP22:52 10 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 2 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project