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();
}
}
}