SQL Server Structure Access
-
Does anyone know if it is possible, using C# and the .NET framework, to get access to the structure of databases in SQL Server? When I say "structure" I mean for example, a programmatic way to get the name of all the tables and their fields and relations, when you know nothing about the database except a connection string for it. Mark Sanders
-
Does anyone know if it is possible, using C# and the .NET framework, to get access to the structure of databases in SQL Server? When I say "structure" I mean for example, a programmatic way to get the name of all the tables and their fields and relations, when you know nothing about the database except a connection string for it. Mark Sanders
Search MSDN for "sysobjects". It's a "table" where this kind of metadata information is available and it's fully documented on MSDN. As a sample, if you want the list of all the tables on the current database, run:
select name from sysobjects where xtype = 'U'
ORACLE One Real A$#h%le Called Lary Ellison
-
Search MSDN for "sysobjects". It's a "table" where this kind of metadata information is available and it's fully documented on MSDN. As a sample, if you want the list of all the tables on the current database, run:
select name from sysobjects where xtype = 'U'
ORACLE One Real A$#h%le Called Lary Ellison
The system tables are subject to change in future versions of SQL server. (They have changed before) The recomended approach is to use the INFORMATION_SCHEMA views and some system stored procedures, since MS have committed to keep the output of these compatible through all versions on SQL Server they exist on, and they will exist on all future releases of SQL Server. (Meaning MS has added some, but not removed any of them...) Usage example:
SELECT * FROM INFORMATION_SCHEMA.TABLES
Lookup in BOL's index on INFORMATION_SCHEMA