微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我使用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");

原文地址:https://www.jb51.cc/vb/255271.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐