CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  C# Table Schema from SqlDataReader

The SqlDataReader 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 C# SqlCommand Object execute , it will instantiate a SqlClient.SqlDataReader Object.

  SqlDataReader sqlReader = sqlCmd.ExecuteReader();

While SqlDataReader is open, you can retrieve the schema information about the current result set using the GetSchemaTable method of SqlDataReader. GetSchemaTable returns a DataTable object, which populated with rows and columns that contain the schema information for the current result set.

         C# Source Code Download           Print Source Code
         C# Table Schema from SqlDataReader - Download
        
C# Tutorial

using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection sqlCnn ;
            SqlCommand sqlCmd ;
            string sql = null;

            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            sql = "Select * from product";

            sqlCnn = new SqlConnection(connetionString);
            try
            {
                sqlCnn.Open();
                sqlCmd = new SqlCommand(sql, sqlCnn);
                SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                DataTable schemaTable = sqlReader.GetSchemaTable();

                foreach (DataRow row in schemaTable.Rows)
                {
                    foreach (DataColumn column in schemaTable.Columns)
                    {
                        MessageBox.Show (string.Format("{0} = {1}", column.ColumnName, row[column]));
                    }
                }
                sqlReader.Close();
                sqlCmd.Dispose();
                sqlCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}



C# ADO.NET data Providers Related Contents
*     C# ADO.NET Connection
*     C# SQl Server Connection
*     C# OLEDB Connection
*     C# ODBC Connection
*     C# ADO.NET Command
*     C# ADO.NET SqlCommand - ExecuteNonQuery
*     C# ADO.NET OleDbCommand - ExecuteNonQuery
*     C# ADO.NET SqlCommand - ExecuteScalar
*     C# ADO.NET OleDbCommand - ExecuteScalar
*     C# ADO.NET SqlCommand - ExecuteReader
*     C# ADO.NET OleDbCommand - ExecuteReader
*     C# ADO.NET DataReader
*     C# ADO.NET SqlDataReader
*     C# ADO.NET OleDbDataReader
*     C# Multiple Result Sets
*     C# Table Schema from OleDbDataReader
*     C# ADO.NET DataAdapter
*     C# ADO.NET SqlDataAdapter
*     C# ADO.NET OleDbDataAdapter
*     C# ExecuteReader and ExecuteNonQuery


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.