我正在研究一个信息系统,这是我的更新语法,它没有显示错误,但它没有更新我的表。有人可以帮忙解决这个问题吗? 顺便说一下,我正在使用VB.Net 2010和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
答案 0 :(得分: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 parameters order and priority以供参考。
最诚挚的问候!
答案 1 :(得分:0)
如果我没记错的话,默认情况下,查询中占位符的名称与参数名称之间没有关系。正如BizApps所说,您应该按照查询中定义的顺序放置参数;这意味着当您将PropertyPayorID
添加到Parameters集合时,它应该是最后的。 Parameters集合的名称仅在本地使用;比如改变各个参数的某些属性。
此外,我不记得你是否可以在查询字符串中使用命名参数作为占位符,或者如果你必须使用?
;像Update PropertiesPayors SET [PayorName]=?, ...
答案 2 :(得分:0)
命令语句.ExecuteNonQuery返回受影响的行数。
这意味着如果您使用了......
intRowsAffected = .ExecuteNonQuery()
返回到变量intRowsAffected的值是ZERO(0)
那么这意味着您的字段PROPERTYID具有相同值的记录(意味着您传递给参数集合的值... PROPERTYPAYORSESSIONID)不存在!
这就是为什么你没有收到任何错误,也没有更新你的数据库。
仔细检查一下...... 你的代码语句.EXECUTENONQUERY()是......
您可以使用以下内容替换它......
intRowsAffected = .ExecuteNonQuery()
Messagebox(intRowsAffected & " Data rows were updated.")
如果消息框显示零行已更新(没有更新行),那么下一步将是手动检查数据库并查看行是否存在,以及它是否具有用于标识的行的SAME键值row - 我假设是property-payor-session-id。
还要记住,session-id很容易随每个会话而改变,而不是一直都是静态的。