Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Get SchemaTable from OleDbDataReader

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb;   public class Form1 : System.Windows.Forms.Form   {         private System.Windows.Forms.Label label1;         private System.Windows.Forms.Button button1;         private System.Windows.Forms.TextBox textBox1;         private System.Windows.Forms.TextBox textBox2;         private System.Windows.Forms.TextBox textBox3;         private System.Windows.Forms.TextBox textBox4;     public Form1()     {             this.label1 = new System.Windows.Forms.Label();             this.button1 = new System.Windows.Forms.Button();             this.textBox1 = new System.Windows.Forms.TextBox();             this.textBox2 = new System.Windows.Forms.TextBox();             this.textBox3 = new System.Windows.Forms.TextBox();             this.textBox4 = new System.Windows.Forms.TextBox();             this.SuspendLayout();             //              // label1             //              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));             this.label1.Location = new System.Drawing.Point(0, 0);             this.label1.Name = "label1";             this.label1.Size = new System.Drawing.Size(232, 48);             this.label1.TabIndex = 0;             this.label1.Text = "Data Readers";             //              // button1             //              this.button1.Location = new System.Drawing.Point(128, 232);             this.button1.Name = "button1";             this.button1.TabIndex = 1;             this.button1.Text = "Read data";             this.button1.Click += new System.EventHandler(this.button1_Click);             //              // textBox1             //              this.textBox1.Location = new System.Drawing.Point(8, 56);             this.textBox1.Multiline = true;             this.textBox1.Name = "textBox1";             this.textBox1.Size = new System.Drawing.Size(72, 152);             this.textBox1.TabIndex = 2;             this.textBox1.Text = "";             //              // textBox2             //              this.textBox2.Location = new System.Drawing.Point(88, 56);             this.textBox2.Multiline = true;             this.textBox2.Name = "textBox2";             this.textBox2.Size = new System.Drawing.Size(72, 152);             this.textBox2.TabIndex = 3;             this.textBox2.Text = "";             //              // textBox3             //              this.textBox3.Location = new System.Drawing.Point(168, 56);             this.textBox3.Multiline = true;             this.textBox3.Name = "textBox3";             this.textBox3.Size = new System.Drawing.Size(72, 152);             this.textBox3.TabIndex = 4;             this.textBox3.Text = "";             //              // textBox4             //              this.textBox4.Location = new System.Drawing.Point(248, 56);             this.textBox4.Multiline = true;             this.textBox4.Name = "textBox4";             this.textBox4.Size = new System.Drawing.Size(72, 152);             this.textBox4.TabIndex = 5;             this.textBox4.Text = "";             //              // Form1             //              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);             this.ClientSize = new System.Drawing.Size(328, 273);             this.Controls.Add(this.textBox4);             this.Controls.Add(this.textBox3);             this.Controls.Add(this.textBox2);             this.Controls.Add(this.textBox1);             this.Controls.Add(this.button1);             this.Controls.Add(this.label1);             this.Name = "Form1";             this.Text = "Form1";             this.ResumeLayout(false);         }     [STAThread]     static void Main()      {       Application.Run(new Form1());     }         private void button1_Click(object sender, System.EventArgs e)         {             string connection1String = "Provider=SQLOLEDB;Data Source=;User ID=sa;Initial Catalog=pubs;";              OleDbConnection connection1 = new OleDbConnection(connection1String);             OleDbCommand command1 = new OleDbCommand("select * from authors", connection1);             connection1.Open();             OleDbDataReader reader1 = command1.ExecuteReader(CommandBehavior.CloseConnection);             DataTable schemaTable = reader1.GetSchemaTable();             textBox1.Text += schemaTable.Rows[0][0].ToString() + "\r\n";             textBox2.Text += schemaTable.Rows[1][0].ToString() + "\r\n";             textBox3.Text += schemaTable.Rows[2][0].ToString() + "\r\n";             textBox4.Text += schemaTable.Rows[3][0].ToString() + "\r\n";             while (reader1.Read())             {                 if (schemaTable.Rows[0][5].ToString() == "System.String") {                     textBox1.Text += reader1.GetString(0) + "\r\n";                 }                 if (schemaTable.Rows[0][5].ToString() == "System.Boolean") {                     textBox1.Text += reader1.GetBoolean(0).ToString() + "\r\n";                 }                 if (schemaTable.Rows[1][5].ToString() == "System.String") {                     textBox2.Text += reader1.GetString(1) + "\r\n";                 }                 if (schemaTable.Rows[1][5].ToString() == "System.Boolean") {                     textBox2.Text += reader1.GetBoolean(1).ToString() + "\r\n";                 }                 if (schemaTable.Rows[2][5].ToString() == "System.String") {                     textBox3.Text += reader1.GetString(2) + "\r\n";                 }                 if (schemaTable.Rows[2][5].ToString() == "System.Boolean") {                     textBox3.Text += reader1.GetBoolean(2).ToString() + "\r\n";                     }                 if (schemaTable.Rows[3][5].ToString() == "System.String")                  {                     textBox4.Text += reader1.GetString(3) + "\r\n";                 }                 if (schemaTable.Rows[3][5].ToString() == "System.Boolean") {                     textBox4.Text += reader1.GetBoolean(3).ToString() + "\r\n";                 }             }             reader1.Close();             connection1.Close();         }   }