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.

OleDbDataReader oledbReader = oledbCmd.ExecuteReader();

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 .

Full Source C#
using System; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } 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); try { oledbCnn.Open(); oledbCmd = new OleDbCommand(sql, oledbCnn); OleDbDataReader oledbReader = oledbCmd.ExecuteReader(); while (oledbReader.Read ()) { MessageBox.Show(oledbReader.GetValue(0) + " - " + oledbReader.GetValue(1) + " - " + oledbReader.GetValue(2)); } oledbReader.Close(); oledbCmd.Dispose(); oledbCnn.Close(); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }