I want to save the last_insert_id() in to a variable so i can use it to pass that id number as reference key in other tables.
Here is my code
private void button5_Click(object sender, EventArgs e)
{
string MyConnectionString = "Server=localhost; Database=markcreations; Uid=root; Pwd=admin";
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd = new MySqlCommand();
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO `customer`(customername,businessname,mobilenumber) values('" + customerName.Text + "','" + businessName.Text + "','" + mobileNumber.Text + "')";
// Here is the Problem I want to extract Last_insert_id() and save it in variable.
String @last_id = cmd.CommandText = ("select Last_insert_id()");
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
cmd = connection.CreateCommand();
cmd.Parameters.AddWithValue("@jobName", row.Cells["Job Name"].Value);
cmd.Parameters.AddWithValue("@flexQuality", row.Cells["Flex Quality"].Value);
cmd.Parameters.AddWithValue("@sizeLength", row.Cells["Size Length"].Value);
cmd.Parameters.AddWithValue("@sizeWidth", row.Cells["Size Width"].Value);
cmd.Parameters.AddWithValue("@rate", row.Cells["Rate"].Value);
cmd.Parameters.AddWithValue("@quantity", row.Cells["Quantity"].Value);
cmd.CommandText = "INSERT INTO `order`(customerId,jobName, flexQuality, sizeLength, sizeWidth, rate, quantity)VALUES( @last_id,@jobName, @flexQuality, @sizeLength, @sizeWidth, @rate, @quantity)";
connection.Open(); // and in the above line i want to insert that variable as values @last_id
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MessageBox.Show("Records inserted.");
}
First, you need to capture the ID with ExecuteScalar. Next, you need to add a parameter to your loop's cmd object for the last_id.
string MyConnectionString = "Server=localhost; Database=markcreations; Uid=root; Pwd=admin";
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd = new MySqlCommand();
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO `customer`(customername,businessname,mobilenumber) values('" + customerName.Text + "','" + businessName.Text + "','" + mobileNumber.Text + "')";
connection.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = ("select Last_insert_id()");
var id = cmd.ExecuteScalar();
connection.Close();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
cmd = connection.CreateCommand();
cmd.Parameters.AddWithValue("@last_id", id);
cmd.Parameters.AddWithValue("@jobName", row.Cells["Job Name"].Value);
cmd.Parameters.AddWithValue("@flexQuality", row.Cells["Flex Quality"].Value);
cmd.Parameters.AddWithValue("@sizeLength", row.Cells["Size Length"].Value);
cmd.Parameters.AddWithValue("@sizeWidth", row.Cells["Size Width"].Value);
cmd.Parameters.AddWithValue("@rate", row.Cells["Rate"].Value);
cmd.Parameters.AddWithValue("@quantity", row.Cells["Quantity"].Value);
cmd.CommandText = "INSERT INTO `order`(customerId,jobName, flexQuality, sizeLength, sizeWidth, rate, quantity)VALUES( @last_id,@jobName, @flexQuality, @sizeLength, @sizeWidth, @rate, @quantity)";
connection.Open(); // and in the above line i want to insert that variable as values @last_id
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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.