Randy Minder
Randy Minder

Reputation: 48502

Retrieving SQL Server Extended Properties From C#

We document our SQL Server database by creating table and column level Description extended properties. We usually enter these via SSMS.

My question is this. I'm creating a C# application where I'd like to read the extended properties for a particular table and its associated columns.

Can someone show me how I might go about doing this?

Thanks - Randy

Upvotes: 3

Views: 4254

Answers (3)

Malick
Malick

Reputation: 6732

A full example for a simple property:

In SQL Server :

enter image description here

Code:

        String strVersion;
        string cmd = "SELECT value  from sys.extended_properties where name = 'MinimumClientVersion'";
        using (var connection = new SqlConnection(connectionString))
        using (var comm = new SqlCommand(cmd, connection))
        {
            connection.Open();
            strVersion = (string)comm.ExecuteScalar();
            connection.Close();
        }
        Version MinimumVersion = new Version(strVersion);

Upvotes: 0

Remus Rusanu
Remus Rusanu

Reputation: 294407

You simply ask for them using the built-in fn_listextendedproperty. The result of this function is an ordinary table result set that you read in C# using your data access tool of choice (SqlCommand/SqlDataReader, linq, datasets etc).

Upvotes: 7

Nathan Wheeler
Nathan Wheeler

Reputation: 5932

Read this: Extract SQL Column Extended Properties From LINQ in C# and see if that's something you could do in your situation.

Upvotes: 1

Related Questions