繁体   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#?

我使用MySQL ODBC将数据插入MySQL表。 表中的第一列是int类型和自动递增的ID。 当我插入第一行的数据时,@ ReqID的值应该是什么,如下所示? 另外,如何确保后续执行为ID自动递增?

这是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();
            }
        }

在MySQL中,如果要使用数据库本身的自动递增功能,则不应在INSERT期间提供ID字段。

那就是你的情况。

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

这样,数据库将插入一个新行,并为您提供下一个可用的ID。

由于其他人已经回答了你的问题,我建议你在项目中使用MySQL Connector而不是使用OLEDB对象。 下载并安装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();
     }
}

可能这会解决问题。

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

暂无
暂无

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

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