Wednesday, February 17, 2010

Get Alias and Base name of Columns

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

using System.Data;

 

namespace GetColumnName

{

    class Program

    {

        static void Main(string[] args)

        {

            cn.ConnectionString = "server=******;Integrated Security=false;database=******;Connection Lifetime=300;User ID=******;Password=******";

            cn.Open();

 

            //Retrieve records from the Employees table into a DataReader.

            cmd.Connection = cn;

            cmd.CommandText = "SELECT name as empname FROM Employee";

            myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

 

            //Retrieve column schema into a DataTable.

            schemaTable = myReader.GetSchemaTable();

 

            //For each field in the table...

            foreach (DataRow myField in schemaTable.Rows){

            //For each property of the field...

                foreach (DataColumn myProperty in schemaTable.Columns) {

                //Display the field name and value.

                    Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());

                }

                Console.WriteLine();

 

                //Pause.

                Console.ReadLine();

            }

 

            //Always close the DataReader and connection.

            myReader.Close();

            cn.Close();

 

        }

    }

}

 

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code