如何一次执行四个查询然后检查成功或失败?
Posted
技术标签:
【中文标题】如何一次执行四个查询然后检查成功或失败?【英文标题】:How to execute four queries once and then check success or failure? 【发布时间】:2015-06-15 14:59:08 【问题描述】:我需要执行四个查询,然后如果成功必须返回 true,否则返回 false。
查询影响数据库,但函数返回 false
Private Function save_to_data()
Dim success As Boolean = False
Dim conn As OleDbConnection = GetDbConnection()
Dim total_due As Decimal = sanitize(txt_total_due.Text)
Dim amount_paid As Decimal = sanitize(txt_due.Text)
Dim discount As Decimal = sanitize(txt_discount.Text)
Dim balance As Decimal = sanitize(txt_balance.Text)
Dim cmdfoods As New OleDbCommand("UPDATE foods SET status='billed' WHERE customer_id = " & lbl_id.Text & "", conn)
Dim cmdservices As New OleDbCommand("UPDATE services SET status = 'billed' WHERE customer_id = " & lbl_id.Text & "", conn)
Dim cmdreservations As New OleDbCommand("UPDATE reservations SET nights = 4 WHERE customerid = " & lbl_id.Text & "", conn)
Dim bill As New OleDbCommand("INSERT INTO bills(customer_id, accomendation, food, service, total_due, amount_paid, discount, balance, transaction_date) VALUES " & _
"(" & lbl_id.Text & ", " & accomendation_total & ", " & food_total & ", " & service_total & ", " & total_due & ", " & amount_paid & " " & _
", " & discount & ", " & balance & ", '" & Date.Now & "')", conn)
conn.Open()
If cmdfoods.ExecuteNonQuery And cmdservices.ExecuteNonQuery And cmdreservations.ExecuteNonQuery And bill.ExecuteNonQuery Then
success = True
Else
success = False
End If
conn.Close()
Return success
End Function
【问题讨论】:
在打开连接后将And
更改为AndAlso
...还在每个ExecuteNonQuery
之后添加>0
,因为此方法返回受影响的行数...您的方式正在评估条件不起作用,因为ExecuteNonQuery doesn't return a Boolean
。
如果您的数据库支持存储过程,最好将所有四个查询放在一个中。特别是如果您需要回滚。
MS Access 2007 是否支持存储过程,我正在使用扩展名为 .accdb 的 MS Access 文件
这里的代码容易受到sql注入攻击。
@saedawke 没有用于 Access 的存储过程对不起...
【参考方案1】:
代码还有许多其他问题(sql 注入、在多个命令之间共享连接),但这是朝着正确方向迈出的一步:
Try
conn.Open()
cmdfoods.ExecuteNonQuery()
cmdservices.ExecuteNonQuery()
cmdreservations.ExecuteNonQuery()
bill.ExecuteNonQuery()
success = True
Catch
success = False
Finally
conn.Close()
End Try
更完整的解决方案:
Private Function save_to_data()
Dim sql As String = _
"UPDATE foods SET status='billed' WHERE customer_id = ? ;" & _
"UPDATE services SET status = 'billed' WHERE customer_id = ? ;" & _
"UPDATE reservations SET nights = 4 WHERE customerid = ? ;"
"INSERT INTO bills(" & _
"customer_id, accomendation, food, service, total_due, amount_paid, discount, balance, transaction_date" & _
") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?);"
Using conn As OleDbConnection = GetDbConnection(), _
cmd As New OleDbCommand(sql, conn)
cmd.Parameters.Add("food_customer_id", OleDbType.Integer).Value = lbl_id.Text
cmd.Parameters.Add("serv_customer_id", OleDbType.Integer).Value = lbl_id.Text
cmd.Parameters.Add("res_customer_id", OleDbType.Integer).Value = lbl_id.Text
cmd.Parameters.Add("bill_customer_id", OleDbType.Integer).Value = lbl_id.Text
cmd.Parameters.Add("accomendataion", OleDbType.VarChar).Value = accomendation_total
cmd.Parameters.Add("food_total", OleDbType.Decimal).Value = food_total
cmd.Parameters.Add("service_total", OleDbType.Decimal).Value = service_total
cmd.Parameters.Add("total_due", OleDbType.Decimal).Value = total_due
cmd.Parameters.Add("amount_paid", OleDbType.Decimal).Value = amount_paid
cmd.Parameters.Add("discount", OleDbType.Decimal).Value = discount
cmd.Parameters.Add("balance", OleDbType.Decimal).Value = balance
cmd.Parameters.Add("transaction_date", OleDbType.Date).Value = Date.Now
conn.Open()
Return (cmd.ExecuteNonQuery() > 0)
End Using
End Function
【讨论】:
试图投票,但我必须有更多的声誉。 @Joel Coehoorn -- 如果不更新行,它会抛出异常吗?我的印象是只有在出现问题时才会触发异常。因为不满足条件而不更新行不是“问题”,对吧?【参考方案2】:ExecuteNonQuery 不返回布尔值,而是一个表示受插入/删除/更新语句影响的行的整数。
If cmdfoods.ExecuteNonQuery() > 0 AndAlso cmdservices.ExecuteNonQuery() > 0 AndAlso cmdreservations.ExecuteNonQuery() > 0 AndAlso bill.ExecuteNonQuery() > 0 Then
success = True
Else
success = False
End If
请看:https://***.com/a/5349149/3185357
【讨论】:
没有。除非我在寻找额外的信息,否则我不会阅读 cmets。我看看我是否可以回答这个问题,并看看其他人是否已经回答了。不知道这有什么价值,但对每个人来说都是如此。以上是关于如何一次执行四个查询然后检查成功或失败?的主要内容,如果未能解决你的问题,请参考以下文章