Is there a way to query the database to retrieve information about the schema of a table? I’m interested in just getting a list of the Column names and whether they are primary keys; is this possible? I don’t care about the type, just its name and whether it is a primary key.
Sample Table:
Table Organism
{
primary: int ID;
int Kingdom;
int Phylum;
int Class;
int Genus;
int Species;
nvarchar(50) Name;
}
Sample Usage:
List<Tuple<string, bool>> t = ReadTable("Organism");
t.ForEach(x => Console.WriteLine(x.Item2 ? x.Item2 + ": " + x.Item1 : x.Item1));
Sample Output:
True ID
Kingdom
Phylum
Class
Genus
Species
Name
I am using C#4.0 and SQL Server 2008 R2. I think this should be possible using the system tables but I don’t know how to do it.
The system views contain a wealth of data and they are easy to use.