Search

Get Scema table

//get schema table
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace GetScemaTable
{
    public partial class Form1 : Form
    {
        public string _connStr_ = "Data Source=127.0.0.1,1433;Initial Catalog=killercode;User Id=sa;Password=iamadmin;";
        SqlCommand scmd;
        SqlConnection conn;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(_connStr_);
                scmd = new SqlCommand("select * from grademstr", conn);

                scmd.Connection.Open();
                SqlDataReader sdr = scmd.ExecuteReader();
                DataTable gst = sdr.GetSchemaTable();

                foreach (DataRow item in gst.Rows)
                {
                    foreach (DataColumn col in gst.Columns)
                    {
                        MessageBox.Show(string.Format("{0} = {1}", col.ColumnName, item[col]));
                    }
                }
                sdr.Close();
                scmd.Dispose();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}