更新OleDb时未保存记录

时间:2016-09-24 01:40:09

标签: database vb.net 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

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

  

员工的信息已成功更新!

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

1 个答案:

答案 0 :(得分:0)

始终检查"new_credit"=>"{\"balance\":3.102,\"currency\":\"usd\"}", "tags"=>["_grokparsefailure"]}, @metadata_accessors=#<LogStash::Util::Accessors:0x46761362 @store={"path"=>"/var/log/nginx/access.log.1"}, @lut={"[path]"=>[{"path"=>"/var/log/nginx/access.log.1"}, "path"]}>, @cancelled=false>], :response=>{"create"=>{"_index"=>"logstash-api-2016.09.27", "_type"=>"logs", "_id"=>"AVdqrION3CJVjhZgZcnl", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse [new_credit]", "caused_by"=>{"type"=>"number_format_exception", "reason"=>"For input string: \"{\"balance\":3.102,\"currency\":\"usd\"}\""}}}}, :level=>:warn 的结果。这将返回修改的行数。

command.ExecuteNonQuery()

如果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)) 放在参数列表的底部,因为它是查询中的最后一个。