vb.net mysql事务回滚多次插入失败

时间:2017-09-19 09:21:59

标签: mysql vb.net transactions

我正在开发pos应用程序并在几个月内遇到与mysql inserts的事务问题。有些时候主表数据被存储并且详细信息表被回滚在多个表中插入数据(master-details)。我已经给出了下面的代码请有人帮我解决这个问题吗?

    Dim tr As MySqlTransaction
    Try
                   tr = con.BeginTransaction
                   Dim recordcount As Integer = 0
                   For i = 0 To Datagridview1.Rows.Count - 1
                            netamount = netamount + Datagridview1.Rows(i).Cells("dgvNetamount").Value
                    qty = qty + Datagridview1.Rows(i).Cells("dgvQty").Value
                    tax = tax + Datagridview1.Rows(i).Cells("dgvTaxamount").Value
                    cm = New MySqlCommand("billing_save", con, tr)
                    cm.CommandType = CommandType.StoredProcedure
                    cm.Parameters.AddWithValue("@Tablename", "sale_bill")
                    cm.Parameters.AddWithValue("@Operation", "INSERT")
                    cm.Parameters.AddWithValue("@PRObillno", txtBillno.Text.Trim)
                    cm.Parameters.AddWithValue("@PRObillprice", Datagridview1.Rows(i).Cells("dgvRate").Value)
                    cm.Parameters.AddWithValue("@PROqty", Datagridview1.Rows(i).Cells("dgvQty").Value)
                    cm.Parameters.AddWithValue("@PAuto_id", 0)
                    cm.Parameters.AddWithValue("@PROdiscountpercentage", Datagridview1.Rows(i).Cells("dgvDiscountPercentage").Value)
                    cm.Parameters.AddWithValue("@PROdiscount", Datagridview1.Rows(i).Cells("dgvDiscountamt").Value)
                    cm.Parameters.AddWithValue("@PROsgst", Datagridview1.Rows(i).Cells("dgvTax").Value)
                    cm.Parameters.AddWithValue("@PROcgst", Datagridview1.Rows(i).Cells("dgvCGST").Value)
                  recordcount += cm.ExecuteNonQuery()
               Next
            'summary of billing
                cm = New MySqlCommand("billing_summary_save", con, tr)
                cm.CommandType = CommandType.StoredProcedure
                cm.Parameters.AddWithValue("@Tablename", "sale_bill")
                cm.Parameters.AddWithValue("@CustomerId", dgCustomer.Rows(0).Cells("dgvCustomerId").Value)
                cm.Parameters.AddWithValue("@Operation", "INSERT")
                cm.Parameters.AddWithValue("@PBillno", txtBillno.Text.Trim)
                cm.Parameters.AddWithValue("@Billdate", Format(dtBillDate.Value, "yyyy-MM-dd HH:mm"))
                cm.Parameters.AddWithValue("@Billstatus", cmbMop.SelectedIndex)
                cm.Parameters.AddWithValue("@OrderNo", txtOrderNo.Text)
                cm.Parameters.AddWithValue("@paymentdate", Format(dtBillDate.Value, "yyyy-MM-dd HH:mm"))
                cm.ExecuteNonQuery()
                'update billno
                cm = New MySqlCommand("update bno set billno='" & billno & "'", con, tr)
                cm.ExecuteNonQuery()
    Catch ex As Exception
        tr.Rollback()
        MessageBox.Show(ex.ToString)
    Finally
        cm.Dispose()
        ad.Dispose()
        tr.Dispose()
    End Try

0 个答案:

没有答案