Home

Data Binding: The OLE DB Data Reader

 

Introduction

A data command is used to initiate an action to perform on a database. To read data of a database, one of the objects you can use is called a data reader. 

If you use a data reader to read some values, the first value is read, followed by the second value, and so on.

If you are planning to use a Microsoft Access database, you can use the OLE DB data reader, represented by a class named OleDbDataReader. To get a data reader, you can declare a variable of type OleDataReader.

To provide data to the reader, the OleDbCommand class is equipped with the ExecuteReader() method that is overloaded with two versions. The simplest version of this method uses the following syntax:

public OleDbDataReader ExecuteReader();

Before using a data reader, you should first create a command that would specify how data would be acquired. Once the data is read, you can pass it to the data reader by assigning the result of a call to a OleDbCommand.ExecuteReader() method to a OleDbDataReader object.

Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection Connect =
    new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
              "Data Source=" + Server.MapPath("exercise.mdb")))
        {
            string strItems =
                "SELECT * FROM Customers WHERE AccountNumber = '72-644';";
            OleDbCommand cmdEmployees =
                new OleDbCommand(strItems, Connect);

            Connect.Open();

            OleDbDataReader rdr = cmdEmployees.ExecuteReader();
        }
    }
}

Using an OLE DB Data Reader

Once data is supplied to the reader, you can access it, one value at a time, from top to bottom. To access data that the reader acquired, you can call its Read() method whose syntax is:

public override bool Read();

When reading the values, as mentioned already many times, the data reader reads one value at a time and moves to the next.

As you read and access each value by its index, you can retrieve it and do what you want with it. For example you can assign it to a web control to display to the web page visitor.

When reading the records of a table, the data reader reads one record at a time and moves to the next. Before moving to the next record, you can access the values stored in the current record. To help with this, the columns of the table being read are stored in a collection and each column can be referred to with a numeric index. The first column has an index of 1.

Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection Connect =
    new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
              "Data Source=" + Server.MapPath("exercise.mdb")))
        {
            string strItems =
                "SELECT * FROM Customers WHERE AccountNumber = '427006';";
            OleDbCommand cmdCustomers =
                new OleDbCommand(strItems, Connect);

            Connect.Open();

            OleDbDataReader rdr = cmdCustomers.ExecuteReader();

            while (rdr.Read())
                txtFirstName.Text = rdr[1].ToString();
        }
    }
}
 
 
 

The second column has an index of 2, and so on. Here is an example that gets the values of different fields:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection Connect =
    new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
              "Data Source=" + Server.MapPath("exercise.mdb")))
        {
            string strItems =
                "SELECT * FROM Customers WHERE AccountNumber = '427006';";
            OleDbCommand cmdCustomers =
                new OleDbCommand(strItems, Connect);

            Connect.Open();

            OleDbDataReader rdr = cmdCustomers.ExecuteReader();

            while (rdr.Read())
            {
                txtAccountNumber.Text = rdr[0].ToString();
                txtFirstName.Text     = rdr[1].ToString();
                txtLastName.Text      = rdr[2].ToString();
            }
        }
    }
}

To retrieve the actual data stored in a column, you may need to know the type of information the column is holding so you can read it accurately. Before sending the data, you must convert the value read to the appropriate (and probably exact) format. Here are examples:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection Connect =
    new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
              "Data Source=" + Server.MapPath("exercise.mdb")))
        {
            string strItems =
                "SELECT * FROM Customers WHERE AccountNumber = '427006';";
            OleDbCommand cmdCustomers =
                new OleDbCommand(strItems, Connect);

            Connect.Open();

            OleDbDataReader rdr = cmdCustomers.ExecuteReader();

            while (rdr.Read())
            {
                txtAccountNumber.Text = rdr.GetString(0);
                txtFirstName.Text     = rdr.GetString(1);
                txtLastName.Text      = rdr.GetString(2);
            }
        }
    }
}
 

 

   
 

Home Copyright © 2009-2016, FunctionX, Inc.