星期五, 6月 09, 2017

[ADO] Schema 資訊

利用 SqlDataAdapter.FillSchema() 填入 DataTable Schema 資訊,仔細確認才發現到 DataColumn 內沒有 SqlDbType 資訊,要利用 Command.ExecuteReader(CommandBehavior.KeyInfo) 才會有 SqlDbType 資訊
using System.Data.SqlClient;

namespace SchemaInfo
{
    public partial class FrmSchemaInfo : Form
    {
        public FrmSchemaInfo()
        {
            InitializeComponent();

            dgvSchemaInfo.AutoGenerateColumns = false;
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            string ConnString = @"Data Source=.\sql2016;Initial Catalog=AdventureWorks2016CTP3;Integrated Security=True;";
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM HumanResources.Employee", conn);
                conn.Open();

                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo))
                {
                    DataTable SchemaTable = reader.GetSchemaTable();
                    dgvSchemaInfo.DataSource = SchemaTable;
                };
            }
        }
    }
}
只列出和 SqlDataAdapter.FillSchema() 有差異,覺得用的到欄位資訊喔

[ADO] Schema 資訊

沒有留言:

張貼留言