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

前端 未结 5 654
天命终不由人
天命终不由人 2021-02-08 22:40

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 specif

5条回答
  •  猫巷女王i
    2021-02-08 22:49

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

    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:

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

    Update 2: And the complete example how to find a row using primary keys with a DataTable already filled with data:

    'Define primary keys (you do this only once)
    table.PrimaryKey = New DataColumn() {table.Columns("column1"), _
                                         table.Columns("column2")}
    'Find a row:
    Dim MyDataRow As DataRow
    MyDataRow = table.Rows.Find(New Object() {value1, value2}) '<- DataRow returned
    If MyDataRow IsNot Nothing Then 'If a row is found
        Return MyDataRow.Item("column3")
    End If
    

提交回复
热议问题