C# ADO.NET OleDbDataReader

OleDbDataReader Object provides a connection oriented data access to the OLEDB Data Sources through C# applications. ExecuteReader() in the OleDbCommand Object sends the commands to OleDbConnection Object and populate an OleDbDataReader Object based on the SQL statements as well as Stored Procedures passed through the OleDbCommand Object.

When the ExecuteReader method in OleDbCommand Object execute , it will instantiate an OleDb.OleDbDataReader Object. When we started to read from an OleDbDataReader it should always be open and positioned prior to the first record. The Read() method in the OleDbDataReader is used to read the rows from the OleDbDataReader and it always moves forward to a new valid row, if any row exist .

using System;
using System.Windows.Forms;
using System.Data.OleDb; 

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            string connetionString = null;
            OleDbConnection oledbCnn ;
            OleDbCommand oledbCmd ;
            string sql = null;

            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
            sql = "Your SQL Statement Here like Select * from product";

            oledbCnn = new OleDbConnection(connetionString);
                oledbCmd = new OleDbCommand(sql, oledbCnn);
                OleDbDataReader oledbReader = oledbCmd.ExecuteReader();
                while (oledbReader.Read ())
                    MessageBox.Show(oledbReader.GetValue(0) + " - " + oledbReader.GetValue(1) + " - " + oledbReader.GetValue(2));
            catch (Exception ex)
                MessageBox.Show("Can not open connection ! ");
} (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About