I am trying to get List of tables and its corresponding columns with 3 constraints :
IsNullable
(I am getting this successfully with the help ofGetSchema
method)IsPrimarykey
(not getting this information)IsAutoincrement
(not getting this information)
Code :
public class Tables
{
public string Name { get; set; }
public List<Columns> Columns { get; set; }
}
public class Columns
{
public string Name { get; set; }
public string IsNullable { get; set; }
public bool IsPrimarykey { get; set; }
public bool IsAutoincrement { get; set; }
}
var list = (from table in connection.GetSchema("Tables").Select()
let name = (string)table["TABLE_NAME"]
let catalog = (string)table["TABLE_CATALOG"]
let schema = (string)table["TABLE_SCHEMA"]
select new Tables
{
Name = schema + "." + name,
Columns =
connection.GetSchema(SqlClientMetaDataCollectionNames.Columns, new[] { catalog, schema, name }).AsEnumerable()
.Select
(
c => new Columns
{
Name =col[3].ToString(),
IsNullable = col.Field<string>("is_nullable"),
IsPrimarykey = ??not getting whether column is primary key or not
IsAutoincrement = ??not getting whether column is autoincrement or not
}
).ToList();
).ToList();
It seems like this is not possible with the GetSchema
method.
How to get primary key and autoincrement information of columns for each of the tables?