C# Table Schema from 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. When the ExecuteReader method in oledbCmd Object execute , it will instantiate an OleDb.OleDbDataReader Object in C#.

While an OleDbDataReader is open, you can retrieve schema information about the current result set using the GetSchemaTable() . GetSchemaTable returns a DataTable object populated with rows and columns that contain the schema information for the current result set.

using System;
using System.Windows.Forms;
using System.Data;
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();
                DataTable schemaTable = oledbReader.GetSchemaTable();
                foreach (DataRow row in schemaTable.Rows)
                    foreach (DataColumn column in schemaTable.Columns)
                        MessageBox.Show (string.Format("{0} = {1}", column.ColumnName, row[column]));
            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