How to read SQL server schema information in .net?

How can I find out in C # a list of tables in a database.

The list of columns that each table has with a full specification, such as column one, is Id and has an int (50) data type, etc.

+3
c # sql-server
source share
3 answers

Use the GetSchema SqlConnection class method :

DataTable t = _conn.GetSchema("Tables"); 

For more information, read the MSDN article Extract Database Schema Information (ADO.NET) . Please note that this will give you results without having to write or directly pass / execute any SQL.

+8
source share

Use information schema views

http://msdn.microsoft.com/en-us/library/ms186778.aspx

Thy is a "standardized way to look at it and contrary to sys tables that are guaranteed not to change (while sys. * Tables are implementation details).

+2
source share

Earlier, we used SQL Server management objects with good success. SMO allows you to easily interact with SQL Server C #. You just need to reference some DLLs and use the object model they provide. For example, to list tables, you can use foreach to iterate through Database.Tables -property.

There is an article in the codeproject that goes through the basics: http://www.codeproject.com/KB/database/SMODemo.aspx

+1
source share

All Articles