Get table schema from SqlDataReader : Column Type Name « 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# / CSharp Tutorial » ADO.Net » Column Type Name 
32.45.2.Get table schema from SqlDataReader
using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
   static void Main(string[] args)
   {
      string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";

      string sql = @"select * from employee ";

      SqlConnection conn = new SqlConnection(connString);

      try
      {
         conn.Open();

         SqlCommand cmd = new SqlCommand(sql, conn);
         SqlDataReader rdr = cmd.ExecuteReader();

         DataTable schema = rdr.GetSchemaTable();

         foreach (DataRow row in schema.Rows
         {
            foreach (DataColumn col in schema.Columns)
               Console.WriteLine(col.ColumnName + " = " + row[col]);
         }
         rdr.Close();
      }
      catch(Exception e)
      {
         Console.WriteLine("Error Occurred: " + e);
      }
      finally
      {
         conn.Close();
      }
   }
}
ColumnName = ID
ColumnOrdinal = 0
ColumnSize = 4
NumericPrecision = 10
NumericScale = 255
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = ID
BaseSchemaName =
BaseTableName =
DataType = System.Int32
AllowDBNull = True
ProviderType = 8
IsAliased =
IsExpression =
IsIdentity = False
IsAutoIncrement = False
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = False
ProviderSpecificDataType = System.Data.SqlTypes.SqlInt32
DataTypeName = int
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 8
ColumnName = FirstName
ColumnOrdinal = 1
ColumnSize = 10
NumericPrecision = 255
NumericScale = 255
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = FirstName
BaseSchemaName =
BaseTableName =
DataType = System.String
AllowDBNull = True
ProviderType = 10
IsAliased =
IsExpression =
IsIdentity = False
IsAutoIncrement = False
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = False
ProviderSpecificDataType = System.Data.SqlTypes.SqlString
DataTypeName = nchar
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 10
ColumnName = LastName
ColumnOrdinal = 2
ColumnSize = 10
NumericPrecision = 255
NumericScale = 255
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = LastName
BaseSchemaName =
BaseTableName =
DataType = System.String
AllowDBNull = True
ProviderType = 10
IsAliased =
IsExpression =
IsIdentity = False
IsAutoIncrement = False
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = False
ProviderSpecificDataType = System.Data.SqlTypes.SqlString
DataTypeName = nchar
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 10
ColumnName = Salary
ColumnOrdinal = 3
ColumnSize = 8
NumericPrecision = 15
NumericScale = 255
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = Salary
BaseSchemaName =
BaseTableName =
DataType = System.Double
AllowDBNull = True
ProviderType = 6
IsAliased =
IsExpression =
IsIdentity = False
IsAutoIncrement = False
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = False
ProviderSpecificDataType = System.Data.SqlTypes.SqlDouble
DataTypeName = float
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 6
ColumnName = Birthday
ColumnOrdinal = 4
ColumnSize = 8
NumericPrecision = 23
NumericScale = 3
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = Birthday
BaseSchemaName =
BaseTableName =
DataType = System.DateTime
AllowDBNull = True
ProviderType = 4
IsAliased =
IsExpression =
IsIdentity = False
IsAutoIncrement = False
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = False
ProviderSpecificDataType = System.Data.SqlTypes.SqlDateTime
DataTypeName = datetime
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 4
32.45.Column Type Name
32.45.1.ResultSet Metadata: table column name and type
32.45.2.Get table schema from SqlDataReader
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.