简体   繁体   中英

Unable to insert data from c# to mysql database

I have this problem. I create 2 tables in MySql which are connected together with foreign key. I want to insert data in the tables from c#.

table persons
id - int, auto increment, primary key
first_name - varchar
last_name - varchar

table addresses
id - auto increment, primary key
city - varchar
steet_number - varchar
persons_id - foreign key

  string mySqlString = "server=localhost;uid=root;pwd=root;database=address_book;";
    MySqlConnection conn = new MySqlConnection(mySqlString);
    try
    {
        conn.Open();

        string insertPerson = "INSERT INTO persons(first_name, last_name) VALUES (@first_name, @last_name)";
        string insertAddress = "INSERT INTO addresses(city, street_number, persons_id) VALUES (@city, @street_number, @persons_id)";

        MySqlCommand command = new MySqlCommand(insertPerson, conn);
        MySqlCommand secondCommand = new MySqlCommand(insertAddress, conn);

        command.Parameters.AddWithValue("@first_name", TextBox1.Text);
        command.Parameters.AddWithValue("@last_name", TextBox2.Text);
        int id = Convert.ToInt32(command.LastInsertedId);

        command.ExecuteNonQuery();

        secondCommand.Parameters.AddWithValue("@city", TextBox3.Text);
        secondCommand.Parameters.AddWithValue("@street_number", TextBox4.Text);
        secondCommand.Parameters.AddWithValue("@persons_id", id);

        secondCommand.ExecuteNonQuery();
    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        conn.Clone();
    }

But this is not working, how should I get the last inserted value from the primary key to insert that value in column "persons_id"? Or maybe I'm wrong somewhere else ?

In one go you can use LAST_INSERT_ID() directly on the query:

string insertPerson = "INSERT INTO persons(first_name, last_name) VALUES (@first_name, @last_name);"
                    + "INSERT INTO addresses(city, street_number,persons_id) VALUES (@city, @street_number, LAST_INSERT_ID());";
MySqlCommand command = new MySqlCommand(insertPerson, conn);
command.Parameters.AddWithValue("@first_name", TextBox1.Text);
command.Parameters.AddWithValue("@last_name", TextBox2.Text);
command.Parameters.AddWithValue("@city", TextBox3.Text);
command.Parameters.AddWithValue("@street_number", TextBox4.Text);
command.ExecuteNonQuery();

Alternatively you can run 2 commands 1 to insert the person and retrieve the ID and the other to insert the addresses:

string insertPerson = "INSERT INTO persons(first_name, last_name) VALUES (@first_name, @last_name)";
MySqlCommand command = new MySqlCommand(insertPerson, conn);
command.Parameters.AddWithValue("@first_name", TextBox1.Text);
command.Parameters.AddWithValue("@last_name", TextBox2.Text);
command.ExecuteNonQuery();
int id = Convert.ToInt32(command.LastInsertedId);

An alternative way would be using the double query like this:

string insertPerson = "INSERT INTO persons(first_name, last_name) VALUES (@first_name, @last_name); SELECT last_insert_id();";
MySqlCommand command = new MySqlCommand(insertPerson, conn);
command.Parameters.AddWithValue("@first_name", TextBox1.Text);
command.Parameters.AddWithValue("@last_name", TextBox2.Text);
int id = Convert.ToInt32(comm.ExecuteScalar());

Then you can use the resulting id on your next query.

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.

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