C# ADO.NET OleDbCommand - ExecuteReader

The ExecuteReader() in OleDbCommand Object sends the SQL statements to the C# Connection Object and populate an OleDbDataReader Object based on the SQL statement. When the ExecuteReader method in OleDbCommand Object execute , it will instantiate an Object of OleDb.OleDbDataReader .

The OleDbDataReader Object is a stream-based , forward-only, read-only retrieval of query results from the Data Source, which do not update the data. The OleDbDataReader cannot be created directly from the code, they can create only by calling the ExecuteReader method of a C# Command Object.

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 cnn ;
            OleDbCommand cmd ;
            string sql = null;
            OleDbDataReader reader ;

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

            cnn = new OleDbConnection(connetionString);
                cmd = new OleDbCommand(sql, cnn);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                    MessageBox.Show(reader.GetValue(0) + " - " + reader.GetValue(1) + " - " + reader.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