vb.net MissingPrimaryKeyException 即使 PK 已设置
vb.net MissingPrimaryKeyException even though PK is set
我正在尝试在 table SPOT
中查找主键 1 的行号。pk 已在 table 设计中设置(旁边有一个键名为 ID 的列),但出现 MissingPrimaryKeyException
错误。我是否需要添加更多代码行来说明哪一列是 pk?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim table As DataTable = New DataTable("SPOT")
Dim foundRow As DataRow = table.Rows.Find("1")
End Sub
End Class
你需要这样的东西:
Private Sub SetPrimaryKeys()
' Create a new DataTable and set two DataColumn objects as primary keys.
Dim table As DataTable = new DataTable()
Dim keys(2) As DataColumn
Dim column As DataColumn
' Create column 1.
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName= "FirstName"
' Add the column to the DataTable.Columns collection.
table.Columns.Add(column)
' Add the column to the array.
keys(0) = column
' Create column 2 and add it to the array.
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName = "LastName"
table.Columns.Add(column)
' Add the column to the array.
keys(1) = column
' Set the PrimaryKeys property to the array.
table.PrimaryKey = keys
End Sub`enter code here`
来自MSDN
我正在尝试在 table SPOT
中查找主键 1 的行号。pk 已在 table 设计中设置(旁边有一个键名为 ID 的列),但出现 MissingPrimaryKeyException
错误。我是否需要添加更多代码行来说明哪一列是 pk?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim table As DataTable = New DataTable("SPOT")
Dim foundRow As DataRow = table.Rows.Find("1")
End Sub
End Class
你需要这样的东西:
Private Sub SetPrimaryKeys()
' Create a new DataTable and set two DataColumn objects as primary keys.
Dim table As DataTable = new DataTable()
Dim keys(2) As DataColumn
Dim column As DataColumn
' Create column 1.
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName= "FirstName"
' Add the column to the DataTable.Columns collection.
table.Columns.Add(column)
' Add the column to the array.
keys(0) = column
' Create column 2 and add it to the array.
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName = "LastName"
table.Columns.Add(column)
' Add the column to the array.
keys(1) = column
' Set the PrimaryKeys property to the array.
table.PrimaryKey = keys
End Sub`enter code here`
来自MSDN