user812775
user812775

Reputation: 153

SMO doesn't retrieve extended properties for index

I'm trying to use SQL Server Management Objects (SMO) to retrieve an extended property on an index, but the retrieved SMO object has an empty ExtentedProperties collection. (The index is on a table.) The extended property is there, I checked in T-SQL. Also, extended properties, e.g. on the database object are found by SMO. All I'm doing is

Server s = new Server(<connectionObj>);
Database db = s.Databases[<databaseName>];
int extCount = db.Tables[<tableName>]
                 .Indexes[<indexName>]
                 .ExtendedProperties
                 .Count

To get

extCount == 0

Am I doing it wrong?

Cheers,

Tilman

PS: It's SQL Server 2005

Upvotes: 0

Views: 739

Answers (2)

Zar Shardan
Zar Shardan

Reputation: 5921

Your code is correct, I suspect your index type is IndexKeyType.DriPrimaryKey for that index and SMO for some strange reason fetches Extended Properties from the primary key object the index supports rather than from the index itself. You can see that if you run your code while in SQL Profiler.

Upvotes: 0

samneric
samneric

Reputation: 3218

You need to refresh the collections before you can reference their items by name - I know - its weird.

Try:

Server s = new Server(<connectionObj>);
Database db = s.Databases[<databaseName>];
db.Tables.Refresh();
db.Tables[<tableName>].Indexes.Refresh();
int extCount = db.Tables[<tableName>]
                 .Indexes[<indexName>]
                 .ExtendedProperties
                 .Count

Upvotes: 4

Related Questions