Net-informations.com
SiteMap  | About    

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 .

  OleDbDataReader reader = cmd.ExecuteReader();

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()
        {
            InitializeComponent();
        }

        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);
            try
            {
                cnn.Open();
                cmd = new OleDbCommand(sql, cnn);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MessageBox.Show(reader.GetValue(0) + " - " + reader.GetValue(1) + " - " + reader.GetValue(2));
                }
                reader.Close();
                cmd.Dispose();
                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.