zoukankan      html  css  js  c++  java
  • DataTable怎么判断一列是否为主键?

    在普通情况下,我们使用SqlDataAdapter来Fill填充DataTable,如果使用下列代码我们是不能拿到主键列的:

    dataadapter.Fill(Table);
    DataColumn[] cols;
    cols = Table.PrimaryKey;
    for(int i = 0; i < cols.Length; i++)
    {
            MessageBox.Show(cols[i].ColumnName);
    }

    因为数据库中的主键约束在普通情况下是不会设置到DataTable中去的。

    解决方法:我们可以加入一句代码,让主键约束设置到DataTable中

    代码:

    dataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    dataadapter.Fill(Table);
    DataColumn[] cols;
    cols = Table.PrimaryKey;
    for(int i = 0; i < cols.Length; i++)
    {
            MessageBox.Show(cols[i].ColumnName);
    }

  • 相关阅读:
    html常用标签_new
    Nginx缓存
    购物车
    css的属性选择
    前端基础之css
    htm基础知识
    TypeScript(1)为什么需要TypeScript
    Electron
    Ant Design
    Umi
  • 原文地址:https://www.cnblogs.com/tianguook/p/3296257.html
Copyright © 2011-2022 走看看