1

Closed

How to get description in table ? I thinks code it errors ?

description

        DatabaseSchema ds;
        string tableName;
        string _friendlyName;
        const string providername = "System.Data.SqlClient";
        const string connectionString =@"Data Source=ass\as;Initial Catalog=da;user id=sa;password=123";
        var dbReader = new DatabaseReader(connectionString, providername);
        ds = dbReader.ReadAll();
        foreach (var d in ds.Tables)
        {
            Console.WriteLine("Description: "+ d.Description);-->Don't Find
        }
Closed Nov 21, 2015 at 4:17 PM by Martinjw

comments

Martinjw wrote Mar 8, 2015 at 1:58 PM

It should work...

In SQLServer, the descriptions are stored in "Extended Properties", conventionally under "MS_Description".
You add them like this:
EXEC sys.sp_addextendedproperty 
@name = N'MS_Description', 
@value = N'Book table.', 
@level0type = N'SCHEMA', @level0name = 'dbo',
@level1type = N'TABLE',  @level1name = 'Book'; --make sure you have a table called Book
GO
The schema reader works by running a special query (in ProviderSchemaReaders/SqlServerSchemaReader.TableDescription):
SELECT 
    SchemaOwner = s.name, 
    TableName = o.name, 
    TableDescription = p.value
FROM sysobjects o
INNER JOIN  sys.schemas s
    ON s.schema_id = o.uid
INNER JOIN sys.extended_properties p 
    ON p.major_id = o.id
    AND p.minor_id = 0
    AND p.name = 'MS_Description'
WHERE 
    o.type= 'U' 
ORDER BY s.name, o.name
If you get nothing, try running that sql query directly against your database. It's possible you're not using "MS_Description" (there's nothing to stop people using other keys, this is just the convention).

If you get an error, please add the exception message and stack trace. Thanks!