简体   繁体   English

如何将Last_insert_id()保存到C#中的变量中

[英]How to save Last_insert_id() in to a variable in c#

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. 我想将last_insert_id()保存到一个变量中,以便可以将其ID号用作其他表中的参考键。

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. 首先,您需要使用ExecuteScalar捕获ID。 Next, you need to add a parameter to your loop's cmd object for the last_id. 接下来,您需要为last_id将参数添加到循环的cmd对象中。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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