I am using MySQL ODBC to insert data into a MySQL table. The first column in the table is an ID that is of type int and auto increments. When I insert the data for the very first row, what should the value be for @ReqID, as shown below? Also, how do I ensure that subsequent executions are auto incrementing for the ID?
Here is the 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.
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.
Since the other have answered your question, I would suggest that you use the MySQL Connector in your project instead of using OLEDB objects. 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:
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;
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.