Click here to Skip to main content
13,896,986 members
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 18 Jul 2006
Licenced CPOL

ADO.NET RecordSet Library

, 18 Jul 2006
Rate this:
Please Sign up or sign in to vote.
An ADO.NET recordset class.


I first started my interaction with databases with MS Access, like most people, and of course, I discovered VBA. OK... I have a background in C, C++, a little of C#, and a couple of other languages, and I thought I could try something new. VBA was so easy to use and understand, and I developed a pretty complex database with a back-end file (data file) and a front-end (distributable interface file) in no time. I used linked tables at first, and with the growing complexity of the database came the problems. First, MS Access took control over the data, recording things when I didn't want them to be changed (for example, calling the move next record command or simply closing a form after changing the data). The second was the speed of the application decreasing due to the network issues.

Thanks to a thing called recordsets, I solved my problem. I built a class in MS Access that allowed me to do two things. It allowed me to do whatever I wanted with the data and was incredibly easy to use. It also allowed me to use snapshot images of the data that boosted the speed of my application over the network. This class I created also did a kind of fake binding of the data and the form controls. By doing this, I regained all control over my data, which avoided the problems of "unwanted recording and data changes" (if you have used MS Access, you know what I'm talking). But the DAO and ADO recordsets are still relatively slow since they need a constant connection. In an already slow network, more than three people connecting at the same time to the database made it sluggish and painful to use.

So I started to study ADO.NET and ... what I discovered was not pretty; Connections, DataAdapters, DataSets, DataTables, DataRows, DataColumns...whhoowww. I was getting dizzy. I asked myself if there was something as simple to use as the DAO or ADO recordset. I couldn't find any. The other thing about ADO.NET is that most articles talk about using wizards that auto-build extensive code specifical to a data format. What if the database structure changes? What do I have to do? Rebuild the form completely???

Based on this, I started building this simple library that simulates a recordset, and another that performs a fake binding...but that's another story.


I don't understand much of ADO.NET. In fact I can't even begin to grasp the amount of features and all the possibilities that it offers, and I may even be saying some really dumb things here.

If you are looking for a miracle solution...forget it. The concept is to allow you to make a simple and basic usage of the ADO.NET features.

If you want to have a very simple database interface layer, this is the place to be.

The main concept

The main concept of these two libraries is to act like middle tiers between the database (tables and relations) and the interface.

Application Layer schematics

The beauty of this is that if you want to change some structural things about your database file (let's say that you want to migrate the technology of your database from MS Access to MS SQL Server), you will only have to change the middle layer(s) without having to change the entire application.

The other great aspect of this is the reduction of the amount of wizard generated code. Making a change in auto generated code is sometimes painful. "I didn't write this. What is this?"

The ADO.NET RecordSet Class

Basically, the recordset works like a pointer to an array. It must offer search capabilities as well as creation, deletion, and saving functionalities. For example, to keep track of where we are, we need to have an integer variable that stores our current position. This index is the key to our recordset, and based on its value, we will run through the data. These are some of the variables used by this class:

'Variables declared inside the class
Private conn As OleDb.OleDbConnection
Private dadapter As OleDb.OleDbDataAdapter
Private cmdbld As OleDb.OleDbCommandBuilder
Private select_command As OleDb.OleDbCommand
Private insert_command As OleDb.OleDbCommand
Private delete_command As OleDb.OleDbCommand
Private update_command As OleDb.OleDbCommand
Private dset As DataSet
Private dtable As DataTable
Private drow As DataRow
Private index As Long
Private newRow As Boolean
Private found As Boolean
Private SQLQuery As String
Private foundrecords_counter As Long
Private operation_finished As Boolean = False

To use the recordset, we have to proceed like we would with the DAO or ADO recordset.

  • Open the connection
  • Open the recordset
  • Do all the operations needed
  • Close the recordset

Opening the connection

The ADO.NET Recordset class has a Connection function which simply stores the path to the database file and the provider to use (and other parameters you might think useful), and tests the connection by opening and closing it.

Function Connection(ByVal constr As String)
        conn.ConnectionString = constr
        Connection = True
    Catch ex As Exception
        MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical + _
               MsgBoxStyle.ApplicationModal + MsgBoxStyle.OkOnly, _
               "ADO.NET RecordSet Library Error")
        Connection = False
    End Try
End Function

Opening and closing the recordset

When the recordset is opened, the index variable is set to 0 (the beginning of the recordset), else it is set to -1 (BOF - Beginning of File, meaning that no record is being pointedat). The SQLstr indicates the SQL query for our table (e.g.: "SELECT * FROM OurTable"). fields is used to retrieve the names of the columns in the table (see ahead). The newRow is used as a flag (see ahead).

To open the recordset, we simply do this:

Function OpenRecordSet(ByVal SQLstr As String)
        dadapter = New OleDb.OleDbDataAdapter(SQLstr, conn)
        dadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        dadapter.MissingMappingAction = MissingMappingAction.Passthrough
        fields = dset.Tables(0).Columns
        If dset.Tables(0).Rows.Count() = 0 Then
            index = -1
            index = 0
        End If
        newRow = False
        OpenRecordSet = True
    Catch ex As Exception
        MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical + _
               MsgBoxStyle.ApplicationModal + MsgBoxStyle.OkOnly, _
               "ADO.NET RecordSet Library Error")
        OpenRecordSet = False
    End Try
End Function

To close the recordset, we simply do this:

Function CloseRecordSet() As Boolean
        CloseRecordSet = True
    Catch ex As Exception
        MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical + _
               MsgBoxStyle.ApplicationModal + _
               MsgBoxStyle.OkOnly, "ADO.NET RecordSet Library Error")
        CloseRecordSet = False
    End Try
End Function

So to use this class, we just have to do something like this:

  • create and initialize our variable (named, for example "rs")
  • rs.Connection("parameters of the connection go here")
  • rs.OpenRecordSet("the SQL Query that defines our table")

This is great, but how do I retrieve/set values in a recordset?

Let's see how we can do this.

Retrieving/setting values of a recordset

To return/set the value of a field in the recordset, we create a property called FieldValue and indicate the name or index of the column to return/set. For example, one of the overloaded implementations of this property is like this:

Property FieldValue(ByVal columnName As String)
            FieldValue = dset.Tables(0).Rows(index).Item(columnName)
        Catch ex As Exception
            MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical + _
                   MsgBoxStyle.ApplicationModal + MsgBoxStyle.OkOnly, _
                   "ADO.NET RecordSet Library Error")
        End Try
    End Get
    Set(ByVal Value)
            If newRow Then
                drow.Item(columnName) = Value
                dset.Tables(0).Rows(index).Item(columnName) = Value
            End If
        Catch ex As Exception
            MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical + _
                   MsgBoxStyle.ApplicationModal + MsgBoxStyle.OkOnly, _
                   "ADO.NET RecordSet Library Error")
        End Try
    End Set
End Property

As you can see, the newRow flag is used here to identify if we are changing an existing record or creating a new one.

Nice, but how do I navigate through the recordset?

Let's see how to do the navigation...

Navigating the recordset

The navigation and searching through the recordset is done using the value of the index. So if we want to go back and forth in the recordset, we simply manipulate that variable.

To navigate the recordset, we build the Move<direction> set of functions.

This is the MoveFirst example:

Sub MoveFirst()
    index = 0
End Sub

Flags on a recordset

Flags are a very important aspect in recordsets. They give you indications on the state of the recordset after a navigation or a search. These indications are usually boolean values that tell you, for example, if you have reached the end of a recordset, or if a search (Find<word> set of functions) was successful or not.

An example of this flag is the BOF flag. This flag is true when the beginning of the recordset has been reached (before the recordset, where there is no data)

You can get this flag as true, for example, if you continuously do a MovePrevious command and pass beyond the first position.

ReadOnly Property BOF() As Boolean
        If index < 0 Then
            BOF = True
            BOF = False
        End If
    End Get
End Property

The example

I believe the example is very straightforward and easy to understand.

It simply uses some commands of the recordset and some flags, and shows you how this library is used and how easy the recordset functionalities are.


In this article, you have seen how to create a simple recordset class to navigate through data in a database. As for data binding in a form, well...that'll be the next chapter.

Hope this helps. Cheers!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

João Martins
Software Developer
Portugal Portugal
I'm that guy that falls for the impossible...

You may also be interested in...


Comments and Discussions

QuestionAdding parameter in FieldValue property Pin
Mariano Escriu19-May-14 5:56
professionalMariano Escriu19-May-14 5:56 
GeneralMy vote of 5 Pin
IceExplorer7-Dec-11 13:53
memberIceExplorer7-Dec-11 13:53 
GeneralMy vote of 5 Pin
ahmed_aaa323-May-11 15:05
memberahmed_aaa323-May-11 15:05 
GeneralProcurar inspiração no RecordSet Pin
Pedro_FP_Simoes28-Feb-10 15:46
memberPedro_FP_Simoes28-Feb-10 15:46 
GeneralRe: Procurar inspiração no RecordSet Pin
João Martins1-Mar-10 12:05
memberJoão Martins1-Mar-10 12:05 
GeneralRe: Procurar inspiração no RecordSet Pin
Pedro_FP_Simoes2-Mar-10 3:32
memberPedro_FP_Simoes2-Mar-10 3:32 
GeneralRe: Procurar inspiração no RecordSet Pin
João Martins2-Mar-10 12:37
memberJoão Martins2-Mar-10 12:37 
GeneralRe: Procurar inspiração no RecordSet Pin
Pedro_FP_Simoes3-Mar-10 1:01
memberPedro_FP_Simoes3-Mar-10 1:01 
GeneralRe: Procurar inspiração no RecordSet Pin
João Martins3-Mar-10 11:16
memberJoão Martins3-Mar-10 11:16 
GeneralRe: Procurar inspiração no RecordSet Pin
Pedro_FP_Simoes4-Mar-10 1:26
memberPedro_FP_Simoes4-Mar-10 1:26 
GeneralRe: Procurar inspiração no RecordSet Pin
João Martins4-Mar-10 12:57
memberJoão Martins4-Mar-10 12:57 
GeneralRe: Procurar inspiração no RecordSet Pin
Pedro_FP_Simoes7-Mar-10 4:05
memberPedro_FP_Simoes7-Mar-10 4:05 
GeneralThanks Pin
louinsd29-Nov-07 9:16
memberlouinsd29-Nov-07 9:16 
GeneralThanks for your work Pin
linjimu8-Nov-07 22:40
memberlinjimu8-Nov-07 22:40 
Questionfirst record insert to the dataset Pin
Lorenzo20-Aug-07 7:54
memberLorenzo20-Aug-07 7:54 
Generalgood job Pin
tyranno16-Aug-07 9:12
membertyranno16-Aug-07 9:12 
Question! or (Field) Concept [modified] Pin
stsalt27-Jul-07 3:09
memberstsalt27-Jul-07 3:09 
GeneralADO.NET Pin
tgb16-Jan-07 8:12
membertgb16-Jan-07 8:12 
Questiondemo - source Pin
wema20078-Jan-07 2:52
memberwema20078-Jan-07 2:52 
AnswerRe: demo - source Pin
João Martins16-Jan-07 9:15
memberJoão Martins16-Jan-07 9:15 
QuestionHow to delete from recordset Pin
pgkdave15-Nov-06 8:08
memberpgkdave15-Nov-06 8:08 
AnswerRe: How to delete from recordset [modified] Pin
João Martins26-Dec-06 3:22
memberJoão Martins26-Dec-06 3:22 
QuestionADO.NET Pin
HART2127-Jul-06 23:18
memberHART2127-Jul-06 23:18 
AnswerRe: ADO.NET Pin
João Martins1-Aug-06 6:26
memberJoão Martins1-Aug-06 6:26 
GeneralWe are in the same bot Pin
Irwan Hassan26-Jul-06 3:54
memberIrwan Hassan26-Jul-06 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 18 Jul 2006
Article Copyright 2006 by João Martins
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid