0

I'm working on an information system and here's my syntax for update, it shows no errors, but it does not update my table. Anyone can help on this matter? By the way, I'm using VB.Net 2010 and MS Access 2007.

Try
        Dim conn As New OleDbConnection(gConnectionString)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        Try
            Dim comm As New OleDbCommand( "UPDATE PropertiesPayors SET [PayorName]=@PayorName,[LotNumber]=@LotNumber,[LotArea]=@LotArea,[DateOfAward]=@DateOfAward,[DateDueForFullPayment]=@DateDueForFullPayment,[PurchasePrice]=@PurchasePrice,[ReAppraisedValue]=@ReAppraisedValue,[AmountDue]=@AmountDue,[TotalAmountPaid]=@TotalAmountPaid,[AmountUnpaid]=@AmountUnpaid,[PropertyRemarks]=@PropertyRemarks WHERE [PropertyID]=@PropertyPayorID ", conn)
            With comm
                With .Parameters
                    .AddWithValue("@PropertyPropertyID", Val(propertyPayorSessionID.ToString))
                    .AddWithValue("@PayorName", txtPayorName.Text)
                    .AddWithValue("@LotNumber", txtLotNumber.Text)
                    .AddWithValue("@LotArea", Val(txtLotArea.Text))
                    .AddWithValue("@DateOfAward", txtDateOfAward.Text.ToString)
                    .AddWithValue("@DateDueForFullPayment", txtDateOfFullPayment.Text.ToString)
                    .AddWithValue("@PurchasePrice", Val(txtPurchasePrice.Text))
                    .AddWithValue("@ReAppraisedValue", Val(txtReAppraisedValue.Text))
                    .AddWithValue("@AmountDue", Val(txtAmountDue.Text))
                    .AddWithValue("@TotalAmountPaid", Val(txtTotalAmountPaid.Text))
                    .AddWithValue("@AmountUnpaid", Val(txtAmountUnpaid.Text))
                    .AddWithValue("@PropertyRemarks", txtRemarks.Text)
                End With
                .ExecuteNonQuery()
            End With
            msg = MsgBox("Record Updated.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Update Payor")
        Catch myError As Exception
            MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Query Error")
        End Try
    Catch myError As Exception
        MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Connection Error")
    End Try
4

3 回答 3

1

你只是在你的代码上有一个错字

代替

@PropertyPropertyID 

@PropertyPayorID

然后安排与更新语句相同的参数顺序。

试试这个:

    Try
            Dim conn As New OleDbConnection(gConnectionString)
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            Try
                  Dim comm As New OleDbCommand("UPDATE PropertiesPayors SET [PayorName]=@PayorName,[LotNumber]=@LotNumber,[LotArea]=@LotArea,[DateOfAward]=@DateOfAward,[DateDueForFullPayment]=@DateDueForFullPayment,[PurchasePrice]=@PurchasePrice,[ReAppraisedValue]=@ReAppraisedValue,[AmountDue]=@AmountDue,[TotalAmountPaid]=@TotalAmountPaid,[AmountUnpaid]=@AmountUnpaid,[PropertyRemarks]=@PropertyRemarks WHERE [PropertyID]=@PropertyPayorID ", conn)
            With comm
                With .Parameters
                    '.AddWithValue("@PropertyPayorID", Val(propertyPayorSessionID.ToString)) move this to the last part
                    .AddWithValue("@PayorName", txtPayorName.Text)
                    .AddWithValue("@LotNumber", txtLotNumber.Text)
                    .AddWithValue("@LotArea", Val(txtLotArea.Text))
                    .AddWithValue("@DateOfAward", txtDateOfAward.Text.ToString)
                    .AddWithValue("@DateDueForFullPayment", txtDateOfFullPayment.Text.ToString)
                    .AddWithValue("@PurchasePrice", Val(txtPurchasePrice.Text))
                    .AddWithValue("@ReAppraisedValue", Val(txtReAppraisedValue.Text))
                    .AddWithValue("@AmountDue", Val(txtAmountDue.Text))
                    .AddWithValue("@TotalAmountPaid", Val(txtTotalAmountPaid.Text))
                    .AddWithValue("@AmountUnpaid", Val(txtAmountUnpaid.Text))
                    .AddWithValue("@PropertyRemarks", txtRemarks.Text)
                    .AddWithValue("@PropertyPayorID", Val(propertyPayorSessionID.ToString))
                End With
                .ExecuteNonQuery()
            End With
                msg = MsgBox("Record Updated.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Update Payor")
            Catch myError As Exception
                MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Query Error")
            End Try
        Catch myError As Exception
            MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Connection Error")
        End Try

这将解决您的问题。

另请参阅:OleDbCommand 参数顺序和优先级以供参考。

此致!

于 2012-12-26T03:51:20.780 回答
0

命令语句 .ExecuteNonQuery 返回受影响的行数。

这意味着如果您使用...

intRowsAffected = .ExecuteNonQuery()

并且返回到变量 intRowsAffected 的值是零(0)

那么这意味着您的字段 PROPERTYID 具有相同值的记录(意味着您传递到参数集合的值...... PROPERTYPAYORSESSIONID)不存在!

这就是为什么您没有收到任何错误并且您的数据库也没有被更新的原因。

仔细检查一下...您的代码语句 .EXECUTENONQUERY() 在哪里...

您可以将其替换为以下内容...

intRowsAffected = .ExecuteNonQuery()
Messagebox(intRowsAffected & " Data rows were updated.")

如果消息框显示零行已更新(没有行已更新),那么您的下一步将是手动检查数据库并查看该行是否实际存在以及它是否具有您用来识别该行的相同键值 - 其中我假设是财产付款人会话ID。

还要记住,会话 ID 很容易随着每个会话而变化,而不是一直是静态的。

于 2012-12-28T15:29:16.507 回答
0

如果我没记错的话,默认情况下,查询中的占位符名称与参数名称之间没有关系。正如 BizApps 所说,您应该按照查询中定义的顺序放置参数;这意味着PropertyPayorID当您将它添加到您的参数集合时,它应该排在最后。参数集合的名称只能在本地使用;比如改变个别参数的一些属性。

另外,我不记得您是否可以在查询字符串中使用命名参数作为占位符,或者是否必须使用 a?来代替;就像是Update PropertiesPayors SET [PayorName]=?, ...

于 2012-12-26T05:17:20.813 回答