C# Table Schema from OleDbDataReader

The OleDbDataReader Object in C# provides a stream-based, forward-only, read-only retrieval of query results from the Data Source without the ability to update the data.

When the ExecuteReader() method is executed on the OleDbCommand Object, it instantiates an OleDb.OleDbDataReader Object in C#. This OleDbDataReader allows you to efficiently read and access the retrieved data from the Data Source.

OleDbDataReader oledbReader = oledbCmd.ExecuteReader();

OleDbDataReader

While an OleDbDataReader is open and actively reading data, you can retrieve schema information about the current result set using the GetSchemaTable() method. By calling GetSchemaTable(), you can obtain a DataTable object that is populated with rows and columns containing the schema information for the current result set.

The DataTable returned by GetSchemaTable() contains valuable information about the columns in the result set, including details such as column names, data types, sizes, and other relevant metadata. This schema information can be utilized to understand and manipulate the structure of the result set within your application.

Full Source C#
using System; using System.Windows.Forms; using System.Data; 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(); 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])); } } oledbReader.Close(); oledbCmd.Dispose(); oledbCnn.Close(); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }

By examining the schema information provided by GetSchemaTable(), you can programmatically determine the properties and characteristics of the columns in the result set. This enables you to handle the data more effectively and make informed decisions based on its structure.

Conclusion

The OleDbDataReader Object in C# allows for stream-based, forward-only, read-only retrieval of query results. When the ExecuteReader() method is invoked on the OleDbCommand Object, it instantiates the OleDbDataReader. While the OleDbDataReader is open, you can retrieve schema information about the current result set using the GetSchemaTable() method, which returns a DataTable populated with schema details.