Introducing Sets, Cursors, and ADO.NET


Cursors were basically added to SQL to accommodate programmers who preferred row-at-a-time, pointer-style processing. Real relational databases don't even have a notion of record number, for example; you use a SELECT statement to obtain record sets.

The ADO.NET model has been redesigned from scratch. There's no explicit support for cursors in ADO.NET. Why do you need cursors? This is a good question. To get your answer, first you have to find out what cursors do.

In ADO, a recordset represents the set of rows returned from a data source after executing a SQL statement. A recordset uses a cursor to keep track of the current location and navigate through rows.

ADO supports four types of cursors: static, forward-only, keyset, and dynamic. The static cursors are a static copy of records on the client side. Static cursors are either read-only or read/write and provide both backward and forward navigation by using the MoveFirst, MoveNext, MovePrevious, MoveLast, and Move methods of the recordset. Also, you can bookmark records using this type of cursor. The AbsolutePosition property provides the row number of a cursor.

Forward-only cursors are similar to static cursors except they support forward-only navigation from the first to the last record in the cursor. You can update records, insert new records, and delete records, but you can't move backward. The forward-only cursor supports only the MoveNext method of the recordset.

Keyset cursors support both backward and forward navigation through the recordset object's Move methods. In addition to that, they also reflect changes made by other users. Dynamic cursors provide dynamic navigation and reflect all changes immediately. They're useful when your application allows multiple users to add, update, delete, and navigate records simultaneously. Dynamic cursors are flexible, but they don't support absolute positioning and bookmarks.

In ADO, when you create a recordset, you can specify the type of cursors to use. Table A-1 represents the cursor types and their values.

Table A-1: Recordset Cursor Type and Their Values in ADO

CONSTANT

VALUE

DESCRIPTION

adOpenForwardOnly

0

Forward-only recordset cursor (the default)

adOpenKeyset

1

Keyset recordset cursor

AdOpenDynamic

2

Dynamic recordset cursor

AdOpenStatic

3

Static recordset cursor

As you can see, ADO uses cursors to navigate, to position, and to bookmark records in a recordset. ADO.NET represents a new approach. To be blunt, recordsets are history! The DataTable and DataReader have replaced them. Under ADO.NET, a DataReader enables you to perform the function of the forward-only, server-side cursor. However, a DataReader doesn't have a MoveNext method, which moves to the next record of a recordset. But it provides a Read method, which reads all the records until the end of the records. A DataReader is useful when you need to read data fast with no updates and you're not using data-bound controls.

The DataSet and Command objects enable you to work with data as a disconnected source, which is similar to ADO client-side behavior. This method is useful when you need to write data-bound, control-based applications.

ADO.NET doesn't support server-side cursors. However, you can use server-side cursors through ADO by adding a reference to the ADODB type library by generating a .NET wrapper for its objects.




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net