Grabbing extended properties from SQL Server into DataTable

谁说胖子不能爱 提交于 2019-12-22 11:34:11

问题


I have a view called PersonOverview which has a bunch of columns; it's a totally normal view, nothing special about it.

I added an extended property called FlexGridHide with a value of 1 to the DatenbereichCD column of that view using.

EXEC sys.sp_addextendedproperty 
        @name = N'FlexGridHide', 
        @value = N'1', 
        @level0type = N'SCHEMA', @level0name = dbo, 
        @level1type = N'VIEW',  @level1name = vPersonOverview,
        @level2type = N'COLUMN', @level2name = DatenbereichCD;

I can find that extended property in SQL Server - no problem - it's there.

But when I load data from the view into a DataTable, I'm obviously not able to actually read out that extended property:

string sqlSelect = @"SELECT TOP 5 DatenbereichCD FROM dbo.vPersonOverview";

DataTable personUebersicht = new DataTable();

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB"].ConnectionString))
using (SqlCommand cmd = new SqlCommand(sqlSelect, con))
using (SqlDataAdapter dap = new SqlDataAdapter(cmd))
{
   dap.Fill(personUebersicht);
}

DataColumn datenbereichCD = personUebersicht.Columns["DatenbereichCD"];    
int extendedProps = datenbereichCD.ExtendedProperties.Count;

The connection works just fine, the query gets executed just fine, returns five rows as expected, the column is present, and everything seems fine - except I don't get any values in the ExtendedProperties collection - the .Count is always 0.

Any ideas? Is there anything I can do to actually get those extended properties? Connection string parameter or a property on the SqlCommand or something obscure?

Update: to the untrained ignorant, having Extended Properties on SQL Server columns, and Extended Properties on the ADO.NET DataColumn type sort of suggested that those SQL Server extended properties would be loaded into the ADO.NET extended properties - but that really doesn't seem to be the case - ah well.....

I ended up using a second query, as Kevin suggested - but since I need to get extended properties for both tables and views and you have to specify what you're looking for using the fn_listextendedproperty function, I instead chose to query the sys.extended_properties system catalog view for the information I need. This here is my query that I'm using to get the information I need about the extended properties from SQL Server:

SELECT 
    ep.class, ep.class_desc, ep.name, ep.value,
    SchemaName = s.name,
    ObjectName = o.name,
    ColumnName = c.Name,
    ObjectType = o.type, 
    ObjectTypeDesc = o.type_desc
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON ep.major_id = o.object_id
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
INNER JOIN sys.columns c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id

回答1:


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"]));
}


来源:https://stackoverflow.com/questions/11331336/grabbing-extended-properties-from-sql-server-into-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!