Get SchemaTable from OleDbDataReader : OleDbDataReader « ADO.Net « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » OleDbDataReader 
32.11.5.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(00);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(23248);
            this.label1.TabIndex = 0;
            this.label1.Text = "Data Readers";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(128232);
            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(856);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(72152);
            this.textBox1.TabIndex = 2;
            this.textBox1.Text = "";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(8856);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(72152);
            this.textBox2.TabIndex = 3;
            this.textBox2.Text = "";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(16856);
            this.textBox3.Multiline = true;
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(72152);
            this.textBox3.TabIndex = 4;
            this.textBox3.Text = "";
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(24856);
            this.textBox4.Multiline = true;
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(72152);
            this.textBox4.TabIndex = 5;
            this.textBox4.Text = "";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(328273);
            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();
        }
  }
32.11.OleDbDataReader
32.11.1.Read result set from OleDbDataReader
32.11.2.Use to OleDbDataReader retrieve data
32.11.3.OleDbDataAdapter and SqlDataAdapter: UpdateOleDbDataAdapter and SqlDataAdapter: Update
32.11.4.Typed Accessors
32.11.5.Get SchemaTable from OleDbDataReader
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.