简体   繁体   中英

multiple mysql statements in one transaction c#

In this code I insert into 2 tables tbl_inv and tbl_line . First I insert records into tbl_inv (invoice) and then I insert into tbl_line (invoice lines). When a record is inserted to tbl_inv , invno is auto generated (auto increment column) and then that invno should be inserted in tbl_line table (for all matching records). The following code inserts only into table tbl_inv and not to tbl_line . Any help?

const string InsertStatement1 = @"
    INSERT INTO tbl_inv (cname, date, duedate,subtotal,vat,total) 
    VALUES(@cname, @date, @duedate,@subtotal,@vat,@total )
    ";
            const string InsertStatement2 = @"
    INSERT INTO tbl_line (invno,pname,qty,unitprice,amount) 
    VALUES(@invno,@pname,@qty,@unitprice,@amount )
    ";

            cn.Open();
            using (MySqlTransaction sqlTrans =cn.BeginTransaction())
            using (MySqlCommand sqlCommand = new MySqlCommand(InsertStatement1, cn, sqlTrans))
            {
                sqlCommand.Parameters.Add("@cname", comboBox1.Text);
                sqlCommand.Parameters.Add("@date", dateTimePicker1.Value.ToString("yyyy-MM-dd"));
                sqlCommand.Parameters.Add("@duedate", dateTimePicker2.Value.ToString("yyyy-MM-dd"));
                sqlCommand.Parameters.Add("@subtotal", textBox6.Text);
                sqlCommand.Parameters.Add("@vat", textBox7.Text);
                sqlCommand.Parameters.Add("@total", textBox9.Text);


                sqlCommand.ExecuteNonQuery();

                var last = "select LAST_INSERT_ID()";

                foreach (DataGridViewRow row in mg2.Rows)
                {

                    sqlCommand.Parameters.Add("@invno",last );
                    sqlCommand.Parameters.AddWithValue("@pname", row.Cells[1].Value);
                    sqlCommand.Parameters.AddWithValue("@qty", row.Cells[2].Value);
                    sqlCommand.Parameters.AddWithValue("@unitprice", row.Cells[3].Value);
                    sqlCommand.Parameters.AddWithValue("@amount", row.Cells[4].Value);

                    sqlCommand.CommandText = InsertStatement2;
                }
                sqlTrans.Commit();
            }

I see multiple problems here:

First, you need to execute the LAST_INSERT_ID statement using ExecuteScalar() , and then use the returned value as the parameter value for the next insert.

Second, you aren't executing the next insert statement(s) at all.

So, it should look something like this:

const string InsertStatement1 = @"INSERT INTO tbl_inv (cname, date, duedate, subtotal, vat, total) 
    VALUES (@cname, @date, @duedate, @subtotal, @vat, @total);";
const string InsertStatement2 = @"INSERT INTO tbl_line (invno, pname, qty, unitprice, amount) 
    VALUES (@invno, @pname, @qty, @unitprice, @amount);";

cn.Open();
using (MySqlTransaction sqlTrans = cn.BeginTransaction())
using (MySqlCommand sqlCommand = new MySqlCommand(InsertStatement1, cn, sqlTrans))
{
    sqlCommand.Parameters.Add("@cname", comboBox1.Text);
    sqlCommand.Parameters.Add("@date", dateTimePicker1.Value.ToString("yyyy-MM-dd"));
    sqlCommand.Parameters.Add("@duedate", dateTimePicker2.Value.ToString("yyyy-MM-dd"));
    sqlCommand.Parameters.Add("@subtotal", textBox6.Text);
    sqlCommand.Parameters.Add("@vat", textBox7.Text);
    sqlCommand.Parameters.Add("@total", textBox9.Text);
    sqlCommand.ExecuteNonQuery();

    sqlCommand.Parameters.Clear();
    sqlCommand.CommandText = "SELECT LAST_INSERT_ID();";
    var o = sqlCommand.ExecuteScalar();
    if (o is Int64)
    {
        var id = (Int64)o;
        foreach (DataGridViewRow row in mg2.Rows)
        {
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("@invno", id);
            sqlCommand.Parameters.AddWithValue("@pname", row.Cells[1].Value);
            sqlCommand.Parameters.AddWithValue("@qty", row.Cells[2].Value);
            sqlCommand.Parameters.AddWithValue("@unitprice", row.Cells[3].Value);
            sqlCommand.Parameters.AddWithValue("@amount", row.Cells[4].Value);
            sqlCommand.CommandText = InsertStatement2;
            sqlCommand.ExecuteNonQuery();
        }
    }
    sqlTrans.Commit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM