简体   繁体   English

无法将数据从C#插入mysql数据库

[英]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. 我在MySql中创建2个表,这些表与外键连接在一起。 I want to insert data in the tables from c#. 我想从C#将数据插入表中。

table persons 表人
id - int, auto increment, primary key id-int,自动递增,主键
first_name - varchar first_name-varchar
last_name - varchar last_name-varchar

table addresses 表地址
id - auto increment, primary key id-自动递增,主键
city - varchar 城市-瓦尔恰
steet_number - varchar steet_number-varchar
persons_id - foreign key person_id-外键

  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"? 但这不起作用,如何从主键中获取最后插入的值以将该值插入“ persons_id”列中? Or maybe I'm wrong somewhere else ? 或者,也许我在其他地方错了?

In one go you can use LAST_INSERT_ID() directly on the query: 可以一次性在查询上直接使用LAST_INSERT_ID()

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: 或者,您可以运行2条命令1插入人员并检索ID,另一条命令插入地址:

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. 然后,您可以在下一个查询中使用结果id

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

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