将Transaction应用于多个数据库表。 [英] Apply Transaction to multiple Database Tables.
问题描述
我需要在单个按钮的点击事件上对多个数据库表应用事务。
我正在做什么在下面,但没有工作,请告诉我我做错了什么?
//代码
con.Open();
MySqlTransaction tr = con。 BeginTransaction();
试试
{
barcodeAshoka ba = new barcodeAshoka();
string storeName = lblStoreName.Text;
string itemName = lblNmOnBarCode.Text;
string description = lblDescOnBarCode.Text;
string itemCode = lblItemCode.Text;
string sellingPrice = lblPriceOnBarCode.Text;
string quantity = lblPSW.Text ++ lblMeasureUnit.Text;
string quantity1 = txtStockQty.Text;
string date = dateTimePicker1.Value.To字符串(dd / MM / yyyy);
// ba.printBarcode(发件人,e,NIME - Co-Oprative Store,112,aaa1,asdasd, GIF34567890);
//在GoodsRecieving表中更新StockValue的代码
decimal GoodsRcvgstock = Convert .ToDecimal(txtStockQty.Text);
decimal NewStock = GoodsRcvgstock - Convert.ToDecimal(quantity1);
string strUpdateStock =更新goods_recieving set stockQuantity =''+ NewStock +''where item_category =''+ ddlCategory.Text +''
+and item_nm =''+ ddlItemNm.Text +' '和description =''+ ddlCode.Text +''
+和recieving_date =''+ ddlDate.Text +''和purchase_bill_no =''+ ddlBillNo.Text +''和status =''有效'';
// int i = 0;
MySqlCommand cmd = new MySqlCommand(strUpdateStock,con);
// con.Open();
cmd.Transaction = tr ;
cmd.ExecuteNonQuery();
string strUpdatePckStock =插入tblpackingstocks(item_code,category ,item_name,description,no_of_pkts,packing_date,uom,pkt_wt,bach_no)
+values(''+ lblItemCode.Text +'',''+ ddlCateg2Print.Text +'', ''+ ddlItem2Print.Text +'',''+ ddlDesc2print.Text +''
+,''+ txtLabelQty.Text +'',''+ dateTimePicker1。值+'',''+ lblMeasureUnit.Text +'',''+ lblPSW.Text +'',''+ lblBatchNo.Text +'');
MySqlCommand cmd2 = new MySqlCommand(strUpdatePckStock,con);
cmd2.Transaction = tr;
cmd.Exe cuteNonQuery();
tr.Commit();
//代码打印BarCode
ba.printBarcode(发件人,e,storeName,itemName,description,itemCode,sellPrice,数量,日期);
}
catch(exception ex)
{
tr.Rollback();
MessageBox。显示(ex.Message);
}
终于
{
con.Close();
}
i need to apply transaction on multiple database tables on a click event of single button.
What i m doing is below,but not working,plz tell m what wrong i m doing?
//code
con.Open();
MySqlTransaction tr = con.BeginTransaction();
try
{
barcodeAshoka ba = new barcodeAshoka();
string storeName = lblStoreName.Text;
string itemName = lblNmOnBarCode.Text;
string description = lblDescOnBarCode.Text;
string itemCode = lblItemCode.Text;
string sellingPrice = lblPriceOnBarCode.Text;
string quantity = lblPSW.Text + " " + lblMeasureUnit.Text;
string quantity1 = txtStockQty.Text;
string date = dateTimePicker1.Value.ToString("dd/MM/yyyy");
// ba.printBarcode(sender, e, "NIME - Co- Oprative Store", "112", "aaa1", "asdasd", "GIF34567890");
//code to Update StockValue in GoodsRecieving Table
decimal GoodsRcvgstock = Convert.ToDecimal(txtStockQty.Text);
decimal NewStock = GoodsRcvgstock - Convert.ToDecimal(quantity1);
string strUpdateStock = "Update goods_recieving set stockQuantity = ''" + NewStock + "'' where item_category=''" + ddlCategory.Text + "'' "
+ " and item_nm = ''" + ddlItemNm.Text + "'' and description =''" + ddlCode.Text + "'' "
+ " and recieving_date = ''" + ddlDate.Text + "'' and purchase_bill_no = ''" + ddlBillNo.Text + "'' and status=''active'' ";
// int i = 0;
MySqlCommand cmd = new MySqlCommand(strUpdateStock, con);
// con.Open();
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
string strUpdatePckStock = "Insert into tblpackingstocks(item_code, category, item_name, description, no_of_pkts, packing_date, uom, pkt_wt,bach_no)"
+ " values(''"+lblItemCode.Text+"'',''"+ddlCateg2Print.Text+"'',''"+ddlItem2Print.Text+"'',''"+ddlDesc2print.Text+"'' "
+ " ,''"+txtLabelQty.Text+"'',''"+dateTimePicker1.Value+"'',''"+lblMeasureUnit.Text+"'',''"+lblPSW.Text+"'',''"+lblBatchNo.Text+"'') ";
MySqlCommand cmd2 = new MySqlCommand(strUpdatePckStock,con);
cmd2.Transaction = tr;
cmd.ExecuteNonQuery();
tr.Commit();
//code to print BarCode
ba.printBarcode(sender, e, storeName, itemName, description, itemCode, sellingPrice, quantity, date);
}
catch(Exception ex)
{
tr.Rollback();
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
推荐答案
阅读以下(交易支持)关于如何进行交易: http://zetcode.com/db/mysqlcsharptutorial/ [ ^ ]
Read the following ("transaction support") on how to do transaction : http://zetcode.com/db/mysqlcsharptutorial/[^]
这篇关于将Transaction应用于多个数据库表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!