簡體   English   中英

如何將Last_insert_id()保存到C#中的變量中

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

我想將last_insert_id()保存到一個變量中,以便可以將其ID號用作其他表中的參考鍵。

這是我的代碼

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

首先,您需要使用ExecuteScalar捕獲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