我正在尝试使用3个约束来获取表及其对应列的列表:
IsNullable
(我在GetSchema
方法的帮助下成功完成了这项工作)
IsPrimarykey
(未获取此信息)
IsAutoincrement
(未获取此信息)
代码:
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();
使用GetSchema
方法似乎无法做到这一点。
如何获取每个表的列的主键和自动增量信息?