简体   繁体   English

如何使用带有ASP.Net/C#的MySQL ODBC将自动增量ID插入MySQL表?

[英]How do I insert the auto-increment ID into a MySQL table using MySQL ODBC with ASP.Net/C#?

I am using MySQL ODBC to insert data into a MySQL table. 我使用MySQL ODBC将数据插入MySQL表。 The first column in the table is an ID that is of type int and auto increments. 表中的第一列是int类型和自动递增的ID。 When I insert the data for the very first row, what should the value be for @ReqID, as shown below? 当我插入第一行的数据时,@ ReqID的值应该是什么,如下所示? Also, how do I ensure that subsequent executions are auto incrementing for the ID? 另外,如何确保后续执行为ID自动递增?

Here is the C#: 这是C#:

            string conString = WebConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
        using (OdbcConnection con = new OdbcConnection(conString))
        {
            con.Open();
            using (OdbcCommand cmd = con.CreateCommand()) {
                cmd.CommandText = "INSERT INTO GraphicsRequest (RequestID, Graphic1Desc, Graphic2Desc, Graphic3Desc, ColorChart, Hex1, Hex2, Hex3, Hex4) VALUES (@reqID, @g1d, @g2d, @g3d, @colorChart, @hex1, @hex2, @hex3, @hex4)";
                cmd.Parameters.AddWithValue("@reqID", 1);
                cmd.Parameters.AddWithValue("@g1d", txtGraphic1Desc.Text);
                cmd.Parameters.AddWithValue("@g2d", txtGraphic2Desc.Text);
                cmd.Parameters.AddWithValue("@g3d", txtGraphic3Desc.Text);

                cmd.Parameters.AddWithValue("@colorChart", ddlColorChart.SelectedValue);
                cmd.Parameters.AddWithValue("@hex1", lblColor1.Text);
                cmd.Parameters.AddWithValue("@hex2", lblColor2.Text);
                cmd.Parameters.AddWithValue("@hex3", lblColor3.Text);
                cmd.Parameters.AddWithValue("@hex4", lblColor4.Text);
                cmd.ExecuteNonQuery();
            }
        }

In MySQL you shouldn't supply the ID-field during an INSERT if you want to use the auto incrementing feature of the database itself. 在MySQL中,如果要使用数据库本身的自动递增功能,则不应在INSERT期间提供ID字段。

So that would be in your case. 那就是你的情况。

        string conString = WebConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
    using (OdbcConnection con = new OdbcConnection(conString))
    {
        con.Open();
        using (OdbcCommand cmd = con.CreateCommand()) {
            cmd.CommandText = "INSERT INTO GraphicsRequest (Graphic1Desc, Graphic2Desc, Graphic3Desc, ColorChart, Hex1, Hex2, Hex3, Hex4) VALUES (@g1d, @g2d, @g3d, @colorChart, @hex1, @hex2, @hex3, @hex4)";
            cmd.Parameters.AddWithValue("@g1d", txtGraphic1Desc.Text);
            cmd.Parameters.AddWithValue("@g2d", txtGraphic2Desc.Text);
            cmd.Parameters.AddWithValue("@g3d", txtGraphic3Desc.Text);

            cmd.Parameters.AddWithValue("@colorChart", ddlColorChart.SelectedValue);
            cmd.Parameters.AddWithValue("@hex1", lblColor1.Text);
            cmd.Parameters.AddWithValue("@hex2", lblColor2.Text);
            cmd.Parameters.AddWithValue("@hex3", lblColor3.Text);
            cmd.Parameters.AddWithValue("@hex4", lblColor4.Text);
            cmd.ExecuteNonQuery();
        }
    }

This way the database will INSERT a new row with the next available the ID for you. 这样,数据库将插入一个新行,并为您提供下一个可用的ID。

Since the other have answered your question, I would suggest that you use the MySQL Connector in your project instead of using OLEDB objects. 由于其他人已经回答了你的问题,我建议你在项目中使用MySQL Connector而不是使用OLEDB对象。 Download and install the MySQL Connector and then add a reference in your project to the MySQL.Data extension and then add the MySql.Data.MySqlClient using statement to your class and then update your code as follows: 下载并安装MySQL Connector ,然后将项目中的引用添加到MySQL.Data扩展,然后将MySql.Data.MySqlClient using语句添加到您的类,然后按如下方式更新代码:

string conString = WebConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
using(MySqlConnection con = new MySqlConnection(connectionString))
{
     con.Open();
     using(MySqlCommand cmd = new MySqlCommand() 
     {
            cmd.Connection = con; //<-- It looks like you are missing this line in your code
            cmd.CommandText = "INSERT INTO GraphicsRequest (Graphic1Desc, Graphic2Desc, Graphic3Desc, ColorChart, Hex1, Hex2, Hex3, Hex4) VALUES (@g1d, @g2d, @g3d, @colorChart, @hex1, @hex2, @hex3, @hex4)";
            cmd.Parameters.AddWithValue("@g1d", txtGraphic1Desc.Text);
            cmd.Parameters.AddWithValue("@g2d", txtGraphic2Desc.Text);
            cmd.Parameters.AddWithValue("@g3d", txtGraphic3Desc.Text);
            cmd.Parameters.AddWithValue("@colorChart", ddlColorChart.SelectedValue);
            cmd.Parameters.AddWithValue("@hex1", lblColor1.Text);
            cmd.Parameters.AddWithValue("@hex2", lblColor2.Text);
            cmd.Parameters.AddWithValue("@hex3", lblColor3.Text);
            cmd.Parameters.AddWithValue("@hex4", lblColor4.Text);
            cmd.ExecuteNonQuery();
     }
}

probably this would solve the problem . 可能这会解决问题。

strong textcom.ExecuteNonQuery();
long id = com.LastInsertedId;

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

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