如何获取每个数据库表的列的主键和自动增量信息?

时间:2017-08-02 11:23:52

标签: c# ado.net

我正在尝试使用3个约束来获取表及其对应列的列表:

  1. IsNullable(我在GetSchema方法的帮助下成功完成了这项工作)

  2. IsPrimarykey(未获取此信息)

  3. IsAutoincrement(未获取此信息)

  4. 代码:

    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方法似乎无法做到这一点。

    如何获取每个表的列的主键和自动增量信息?

0 个答案:

没有答案