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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2470681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:47:32  来源:igfitidea点击:

How to define a VB.NET DataTable Column as primary key after creation

vb.netdatatableprimary-key

提问by Olga

I am importing Tables from a Oracle DataBase, using a VB.NET dataAdapter. I use the "fill" command to add the imported data to a DataSet. How is it possible to define a specific column of a DataTable as PrimaryKey, after the DataTable is already filled with data?

我正在使用 VB.NET dataAdapter 从 Oracle 数据库导入表。我使用“fill”命令将导入的数据添加到数据集。在 DataTable 已经填充了数据之后,如何将 DataTable 的特定列定义为 PrimaryKey?

采纳答案by Rob

You can set the primary key of a table by:

您可以通过以下方式设置表的主键:

    Dim table As New DataTable()

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

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

To be able to use the primary key, you need to ensure that all values for the given column are unique.

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

I primarily work in C# and have a couple of Extension methods I use to "tidy" the calls I need to make, which you might want to consider translating to VB and using:

我主要在 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");

回答by Kleinux

As long as the values in the column are unique

只要列中的值是唯一的

table.PrimaryKey = new DataColumn[] { table.Columns["Id"] };

adjust for your column names.

调整您的列名。

回答by Matt Roy

Here is a one-liner in VB (the question was with "using VB.NET"). This example is with 2 columns indexed:

这是 VB 中的一个单行(问题是“使用 VB.NET”)。这个例子有 2 列索引:

table.PrimaryKey = New DataColumn() {table.Columns("column1"), _
                                     table.Columns("column2")}

Update:And here's another one-liner on how to use this 2 columns index to find a row:

更新:这是关于如何使用这 2 列索引查找行的另一个单行:

table.Rows.Find(New Object() {value1, value2}) '<- DataRow returned

回答by CHale

Thanks for the answer Rob - there is a slight issue with the vb version though as the index should be zero-based:

感谢 Rob 的回答 - vb 版本有一个小问题,尽管索引应该是从零开始的:

Dim table As New DataTable()

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

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