vb.net MissingPrimaryKeyException即使设置了PK

时间:2015-12-23 05:37:03

标签: vb.net primary-key rows

我正在尝试在表SPOT中找到主键1的行号。表格设计中已经设置了pk(名称为ID的列旁边有一个键)但我得到{{ 1}}错误。我是否需要添加更多行代码来说明哪个列是pk?

MissingPrimaryKeyException

dataset SPOT table

1 个答案:

答案 0 :(得分:1)

你需要这样的东西:

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