如何使用last insert id传递给另一个表
我有两个表1.product 2.stock我附上了下面的图片。
堆栈表proid是最后一个插入ID来自产品表,它应该在库存表上添加 我无法添加请参阅我在下面尝试的代码
MySqlCommand cm1 = new MySqlCommand();
cm1.Connection = con;
cm1.CommandText = "insert into products(name,qty,price) values (@name,@qty,@price)";
cm1.Parameters.AddWithValue("@name", textBox1.Text);
cm1.Parameters.AddWithValue("@qty", textBox3.Text);
cm1.Parameters.AddWithValue("@price", textBox4.Text);
con.Open();
// Return the id of the new record. Convert from Int64 to Int32 (int).
// return Convert.ToInt32(cm1.Parameters["@newId"].Value);
MySqlCommand cm = new MySqlCommand();
cm.Connection = con;
cm.CommandText = "insert into stock(caname,qty,price) values (@caname,@qty,@price)";
//cm.CommandText = "insert into products(name,qty,price) values (@caname,@qty,@price)";
// cm.Parameters.AddWithValue("@name", textBox1.Text);
// cm.Parameters.AddWithValue("@proid", id);
cm.Parameters.AddWithValue("@caname", textBox2.Text);
cm.Parameters.AddWithValue("@qty", textBox3.Text);
cm.Parameters.AddWithValue("@price", textBox4.Text);
cm.ExecuteNonQuery();
cm1.ExecuteNonQuery();