更新 OleDb 时未保存记录

Records not saved when updating OleDb

我正在使用以下代码更新我在数据库中的记录:

Imports System.Data.OleDb

Public Class Form1

 Private Sub update_Click(sender As Object, e As EventArgs) Handles updateBtn.Click

    '/// UPDATING TABLE - employee

    Try
        Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
            conn.Open()
            Dim command As New OleDbCommand("UPDATE employee SET [Last_Name] = @lname, [First_Name]= @fname, MI = @mi, Bday = @bday, Age = @age, [Nationality] = @natio, [Contact_Number] = @contact, Sex = @gender, Address = @address, Department = @department, [Position] = @position, TIN = @tin, SSS = @sss, Pagibig = @pagibig, PhilHealth = @phh, Termi_Resi = @termiresi, [Date_hired] = @datehired, [Rate_Month] = @rm, [Date_End] = @dateTermiResi, Status = @status, [Father_Name] = @father, [Mother_Name] = @mother, Civil = @civil, Spouse = @spouse, [Number_of_Children] = @numberchild, Child1 = @child1, Child2 = @child2, Child3 = @child3, Child4 = @child4, Child5 = @child5, Child6 = @child6, Child7 = @child7, Child8 = @child8, Child9 = @child9 WHERE [EmployID] = @numberemp", conn)
            With command.Parameters
                .AddWithValue("@numberemp", numberemp.Text)
                .AddWithValue("@lname", lname.Text)
                .AddWithValue("@fname", fname.Text)
                .AddWithValue("@mi", mi.Text)
                .AddWithValue("@bday", bday.Value)
                .AddWithValue("@age", age.Text)
                .AddWithValue("@natio", natio.Text)
                .AddWithValue("@contact", contact.Text)
                .AddWithValue("@gender", gender.Text)
                .AddWithValue("@address", address.Text)
                .AddWithValue("@department", department.Text)
                .AddWithValue("@position", position.Text)
                .AddWithValue("@tim", tin.Text)
                .AddWithValue("@sss", sss.Text)
                .AddWithValue("@pagibig", pagibig.Text)
                .AddWithValue("@phh", phh.Text)
                .AddWithValue("@termiresi", termiresi.Text)
                .AddWithValue("@datehired", datehired.Value)
                .AddWithValue("@rm", rm.Text)
                .AddWithValue("@dateTermiResi", dateTermiResi.Value)
                .AddWithValue("@status", status.Text)
                .AddWithValue("@father", father.Text)
                .AddWithValue("@mother", mother.Text)
                .AddWithValue("@civil", civil.Text)
                .AddWithValue("@spouse", spouse.Text)
                .AddWithValue("@numberchild", numberchild.Text)
                .AddWithValue("@child1", child1.Text)
                .AddWithValue("@child2", child2.Text)
                .AddWithValue("@child3", child3.Text)
                .AddWithValue("@child4", child4.Text)
                .AddWithValue("@child5", child5.Text)
                .AddWithValue("@child6", child6.Text)
                .AddWithValue("@child7", child7.Text)
                .AddWithValue("@child8", child8.Text)
                .AddWithValue("@child9", child9.Text)
            End With
            command.ExecuteNonQuery()
            MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            command.Dispose()
            conn.Close()
            clearall()

            RefreshDGV()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

每当我点击更新按钮时,它都会显示

Employee's Informations Successfuly Updated!

但在数据库中,它不是。我做错了吗?

始终检查 command.ExecuteNonQuery() 的结果。这将 return 修改的行数。

Dim rows = command.ExecuteNonQuery()
If rows = 0 then
    MessageBox.Show("No rows modified")
Else
    MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

如果 rows = 0 那么问题很可能与 where 子句有关。在这种情况下,它匹配参数。

.AddWithValue("@numberemp", numberemp.Text)

尝试使用

.AddWithValue("@numberemp", Integer.Parse(numberemp.Text))

确保类型正确。

还有一些数据库提供商不使用您为参数提供的名称。只关心顺序。因此,将 @numberemp 放在参数列表的底部,因为它是查询中的最后一个。