Linq to SQL - SubmitChanges not working



所以-我有一个MySQL数据库与一个员工表,使用Devart Linq到SQL (MySQL)它在DataContext中有一个主键,我可以加载gridview,选择一行,设置Staff =的实例为ID和Name或gridview中选择的行设置文本框="员工姓名"更改文本框并设置对象Staff。将名称输入到文本框.....

,但我不能提交更改回db!!!!作为一个新手,我可能在做一件愚蠢的事。

Public Class Form1
    Dim db As New MydbContext.MydbDataContext
    Dim bs = New BindingSource
    Dim Staff As New MydbContext.Employee
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' load employee data from MySQL db in datagrid
        Dim StaffQuery = From s In db.Employees Select s
        bs.DataSource = StaffQuery
        DataGridView1.DataSource = bs
        ' this works fine
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        'create an instance of the object Staff with the ID and Name from the datagrid
        'is there a better way to do this?
        Staff.EmployeeID = DataGridView1.Rows(e.RowIndex).Cells("EmployeeID").Value
        Staff.Name = DataGridView1.Rows(e.RowIndex).Cells("Name").Value
        MsgBox(Staff.EmployeeID & " " & Staff.Name)
        TextBox1.Text = Staff.Name
        'this works fine
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Staff.Name = TextBox1.Text   'edit the Name
        MsgBox(Staff.EmployeeID & " " & Staff.Name)  ' this works - shows new Staff.Name from textbox
        db.SubmitChanges()   ' this fails to make db changes
    End Sub
End Class

我认为您需要添加db.Staffs.InsertOnSubmit(Staff)

其中StaffsStaff对象集合的名称。

然而,与其一直保持上下文和对象,为什么不在用户按下保存按钮时创建新的Staff对象呢?

最后,您可以在db上下文中调用GetChangeSet(),以查看挂起的更改。这可以帮助你理解什么时候发生了奇怪的事情

最新更新