Grabbing extended properties from SQL Server into DataTable

十年热恋 提交于 2019-12-06 06:26:32

According to this post the SQL Server extended properties and the ADO.NET extended properties are not related:

The dataColumn.ExtendedProperties is not related to the extended property of column on the SQL server backend.

It appears that you will need to resort to retrieving the extended properties in a separate query.

DataTable personUebersicht = new DataTable();
DataTable extendedProperties = new DataTable();

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB"].ConnectionString))
{
    string sqlSelect = @"SELECT TOP 5 DatenbereichCD FROM dbo.vPersonOverview";
    using (SqlCommand cmd = new SqlCommand(sqlSelect, con))
    using (SqlDataAdapter dap = new SqlDataAdapter(cmd))
    {
        dap.Fill(personUebersicht);
    }

    string sqlProperties = @"SELECT name, value FROM ::fn_listextendedproperty(null,'user','dbo','view','vPersonOverview','column','DatenbereichCD')";
    using (SqlCommand cmd = new SqlCommand(sqlProperties, con))
    using (SqlDataAdapter dap = new SqlDataAdapter(cmd))
    {
        dap.Fill(extendedProperties);
    }
}

// Test the results
foreach (DataRow row in extendedProperties.Rows)
{
    Console.WriteLine(string.Format("{0}: {1}", row["name"], row["value"]));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!