mysql insert into select transaction c 23

Solutions on MaxInterview for mysql insert into select transaction c 23 by the best coders in the world

showing results for - "mysql insert into select transaction c 23"
Linus
04 Aug 2019
1const string InsertStatement1 = @"INSERT INTO tbl_inv (cname, date, duedate, subtotal, vat, total) 
2    VALUES (@cname, @date, @duedate, @subtotal, @vat, @total);";
3const string InsertStatement2 = @"INSERT INTO tbl_line (invno, pname, qty, unitprice, amount) 
4    VALUES (@invno, @pname, @qty, @unitprice, @amount);";
5
6cn.Open();
7using (MySqlTransaction sqlTrans = cn.BeginTransaction())
8using (MySqlCommand sqlCommand = new MySqlCommand(InsertStatement1, cn, sqlTrans))
9{
10    sqlCommand.Parameters.Add("@cname", comboBox1.Text);
11    sqlCommand.Parameters.Add("@date", dateTimePicker1.Value.ToString("yyyy-MM-dd"));
12    sqlCommand.Parameters.Add("@duedate", dateTimePicker2.Value.ToString("yyyy-MM-dd"));
13    sqlCommand.Parameters.Add("@subtotal", textBox6.Text);
14    sqlCommand.Parameters.Add("@vat", textBox7.Text);
15    sqlCommand.Parameters.Add("@total", textBox9.Text);
16    sqlCommand.ExecuteNonQuery();
17
18    sqlCommand.Parameters.Clear();
19    sqlCommand.CommandText = "SELECT LAST_INSERT_ID();";
20    var o = sqlCommand.ExecuteScalar();
21    if (o is Int64)
22    {
23        var id = (Int64)o;
24        foreach (DataGridViewRow row in mg2.Rows)
25        {
26            sqlCommand.Parameters.Clear();
27            sqlCommand.Parameters.AddWithValue("@invno", id);
28            sqlCommand.Parameters.AddWithValue("@pname", row.Cells[1].Value);
29            sqlCommand.Parameters.AddWithValue("@qty", row.Cells[2].Value);
30            sqlCommand.Parameters.AddWithValue("@unitprice", row.Cells[3].Value);
31            sqlCommand.Parameters.AddWithValue("@amount", row.Cells[4].Value);
32            sqlCommand.CommandText = InsertStatement2;
33            sqlCommand.ExecuteNonQuery();
34        }
35    }
36    sqlTrans.Commit();
37}
38
similar questions
queries leading to this page
mysql insert into select transaction c 23