0

With C# and SQL Server 2005 and by using DbConnection.GetSchema() method, I want to get all a table's columns (not of views) only. I have found two collection names related to this

  1. Columns that returns table and views' columns
  2. ViewColumns returns all the view's columns

Neither of above two returns table columns only, nor they have any property to filter Table-columns.

Any help is respected.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user369182
  • 1,995
  • 4
  • 16
  • 17

1 Answers1

0

I don't see any easy way to do this with this particular API you're trying to achieve this with - but why not just use a query like this to get your information?

SELECT
    c.name AS 'ColumName',
    ty.Name AS 'TypeName',
    c.max_length,
    c.is_identity,
    c.is_nullable,
    t.name AS 'TableName'
FROM sys.columns c
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
INNER JOIN sys.tables t ON c.object_id = t.object_id

Just load that into a SqlCommand and execute it against the open connection you have and read the result into some DataTable or other structure for your use. This gives you only table columns - and all of them.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I know we can achieve this in your way, But I am doing all other operations with this method like 1)Getting ForeignKeys with connection.GetSchema("ForeignKeys") 2) Getting Indexes with connection.GetSchema("INDEXES"), same as i want to Get all Table-Columns by this method – user369182 Dec 26 '11 at 09:52
  • @user369182: as I said - I don't think there's any way to get that information you want the way you want. – marc_s Dec 26 '11 at 10:09