加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在创建后将VB.NET DataTable列定义为主键

发布时间:2020-12-17 00:22:37 所属栏目:大数据 来源:网络整理
导读:我使用VB.NET dataAdapter从Oracle DataBase导入表.我使用“fill”命令将导入的数据添加到DataSet.在DataTable已经填充数据之后,如何将DataTable的特定列定义为PrimaryKey? 您可以通过以下方式设置表的主键: Dim table As New DataTable() table.Columns.A
我使用VB.NET dataAdapter从Oracle DataBase导入表.我使用“fill”命令将导入的数据添加到DataSet.在DataTable已经填充数据之后,如何将DataTable的特定列定义为PrimaryKey?
您可以通过以下方式设置表的主键:
Dim table As New DataTable()

    table.Columns.Add(New DataColumn("MyColumn"))

    Dim primaryKey(1) As DataColumn
    primaryKey(1) = table.Columns("MyColumn")
    table.PrimaryKey = primaryKey

为了能够使用主键,您需要确保给定列的所有值都是唯一的.

我主要使用C#并使用一些扩展方法来“整理”我需要进行的调用,您可能需要考虑转换为VB并使用:

public static void SetPrimaryKey(this DataTable value,string columnName)
    {
        value.PrimaryKey = new DataColumn[] { value.Columns[columnName] };
    }

    public static DataRow FindByPrimaryKey(this DataTable value,object key)
    {
        return value.Rows.Find(key);
    }

    // I can then do:

    DataTable table = CallToRoutineThatGetsMyDataTable();
    table.SetPrimaryKey("PKColumnName");
    DataRow result = table.FindByPrimaryKey("valueToFindWith");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读