我正在尝试使用LINQ
. 它不断尝试更新表中的第一条记录。如果我选择第一条记录,它将成功更新该记录,但如果我尝试更新任何其他记录,它表示我正在尝试更改作为 PK 的 CategoryID,因此它不会更改它。
我已经尝试了几种变体,但都没有运气。
Imports System.Data.Linq
Public Class frmUpdate
Dim db As New DataClasses1DataContext
Dim catnum As Integer
Private Sub frmUpdate_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim query = From aCat In db.Categories
Select aCat
CategoryBindingSource.DataSource = query
End Sub
Private Sub btnUpdate_Click_1(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
Try
catnum = CInt(CategoryIDComboBox.Text())
Dim catQuery As Category = (From aCat In db.Categories
Where aCat.CategoryID = catnum
Select aCat).First()
catQuery.CategoryID = catnum
catQuery.CategoryName = CategoryNameTextBox.Text.ToString()
catQuery.Description = DescriptionTextBox.Text.ToString()
db.SubmitChanges()
frmGridView.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class