C#MySql有时插入执行两次或未执行

时间:2016-05-22 22:01:14

标签: c# mysql

我必须在我的MySql中向3个表插入数据,假设它是table1,table2和table3 我的问题是有时候对table2的插入查询执行两次,有时它没有被执行 这是我的代码:

string Query = @"insert into table1(SellCode, SellDate, SellType, Discount, BuyerCode) 
                                    values (@SellCode, @SellDate, @SellType, @Discount, @BuyerCode)";


            using (MySqlConnection conExpDB = new MySqlConnection(ConString))
            using (MySqlCommand cmdExpDB = new MySqlCommand(Query, conExpDB))
            {
                try
                {
                    conExpDB.Open();
                    cmdExpDB.Parameters.AddWithValue("@SellCode", SellCode);
                    cmdExpDB.Parameters.AddWithValue("@SellDate", DateTime.Now);
                    cmdExpDB.Parameters.AddWithValue("@SellType", "Retail");
                    cmdExpDB.Parameters.AddWithValue("@Discount", txtDiscount.Text);
                    cmdExpDB.Parameters.AddWithValue("@BuyerCode", "1");


                    int rowsUpdated = cmdExpDB.ExecuteNonQuery();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }//end try

            }//end using



            string QueryB = @"insert into table2(PaymentCode, PaymentAmount, 
                                    SellCode, PaymentDate) 
                                    values (@PaymentCode, @PaymentAmount, @SellCode, @PaymentDate)";


            using (MySqlConnection conExpDB = new MySqlConnection(ConString))
            using (MySqlCommand cmdExpDB = new MySqlCommand(QueryB, conExpDB))
            {
                try
                {
                    conExpDB.Open();
                    cmdExpDB.Parameters.AddWithValue("@PaymentCode", paymentcode);
                    cmdExpDB.Parameters.AddWithValue("@PaymentAmount", PaymentAmount);
                    cmdExpDB.Parameters.AddWithValue("@SellCode", SellCode);
                    cmdExpDB.Parameters.AddWithValue("@PaymentDate", DateTime.Now);


                    int rowsUpdated = cmdExpDB.ExecuteNonQuery();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }//end try

            }//end using

            foreach (DataGridViewRow row in GridView.Rows)
            {
                string QueryDetail = @"insert into table3(SellDetailCode, SellCode, 
                                    ItemCode, Qty, 
                                    Price) 
                                    values (@SellDetailCode, @SellCode, @ItemCode, @Qty, 
                                    @Price)";

                using (MySqlConnection conExpDB = new MySqlConnection(ConString))
                using (MySqlCommand cmdExpDB = new MySqlCommand(QueryDetail, conExpDB))
                {
                    try
                    {
                        conExpDB.Open();


                        cmdExpDB.Parameters.AddWithValue("@SellDetailCode", SellDetailCode);
                        cmdExpDB.Parameters.AddWithValue("@SellCode", SellCode);
                        cmdExpDB.Parameters.AddWithValue("@ItemCode", ItemCode);
                        cmdExpDB.Parameters.AddWithValue("@Qty", Qty);
                        cmdExpDB.Parameters.AddWithValue("@Price", Price);

                        int rowsUpdated = cmdExpDB.ExecuteNonQuery();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }//end try
                }//end using
            }//end foreach

是否有任何有效的方法来检测插入两次或未执行的查询?或者我的查询有问题吗? 因为它发生得非常罕见,所以很难解决这个错误

0 个答案:

没有答案