简体   繁体   中英

Insert auto increment

I have a method Insert() . Everything is working as expected except for the auto increment. Here's the code:

public void Insert(string m1,int y1,int new_count)
    {
        string query = "INSERT INTO page_counter (id,month,year,page_count) VALUES('','"+m1+"',"+y1+","+new_count+")";

            //create command and assign the query and connection from the constructor
            MySqlCommand cmd = new MySqlCommand(query, connection);

            //Execute command
            cmd.ExecuteNonQuery();

            //close connection
            this.CloseConnection();
    }  

My Id column is an auto-increment. So my question is how can the value be inserted in the database an continue the auto increment in the table for id?

Simply don't specify value for id :

string query = "INSERT INTO page_counter (month,year,page_count) VALUES('"+m1+"',"+y1+","+new_count+")";

And look into better approach, parameterized query, instead of concatenating query string.

All you have to do is exclude the auto-incremented IDENTITY column from your insert.

Change your query to:

//NOTE: We leave the "id" column out of the insert, SQL Server will handle this automatically
string query = "INSERT INTO page_counter (month, year, page_count) VALUES (@Month, @Year, @PageCount)";

SQL will take care of the ID field.

You might notice I used Scalar variables in my query. You can (and should) assign these in the command so that you exclude the possibility of SQL injection:

EDIT DUE TO THE FACT THAT THIS IS COMING FROM MySql.Data.MySqlClient PRE 4.0

MySqlCommand cmd = new MySqlCommand(query, connection);

cmd.Parameters.Add(new MySqlParameter("@Month", m1));
cmd.Parameters.Add(new MySqlParameter("@Year", y1));
cmd.Parameters.Add(new MySqlParameter("@PageCount", new_count));

//Execute the INSERT
cmd.ExecuteNonQuery();

For a little background on SQL Injection I would recommend reading:

  1. SQL Injection on W3Schools
  2. Why use Parameterized SQL on SO

Using AUTO_INCREMENT

No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically.

string query = "INSERT INTO page_counter (month,year,page_count)
VALUES('"+m1+"',"+y1+","+new_count+")";

You can always use Parameterized query to avoid SQL Injection

string query = "INSERT INTO page_counter (month,year,page_count)
VALUES(@month,@year,@page_count)";

cmd.Parameters.AddWithValue("@month",Value1);
cmd.Parameters.AddWithValue("@year", Value2);
cmd.Parameters.AddWithValue("@page_count", Value3);
string query = "INSERT INTO page_counter (month,year,page_count) VALUES('"+m1+"',"+y1+","+new_count+")";

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

您可以在数据库中更改id的标识

"INSERT INTO page_counter (month,year,page_count) VALUES('"+m1+"',"+y1+","+new_count+")"

Do not specify ID from here:

If you are using SQL Server, have your ID field in your DB created like so:

ID int IDENTITY(1,1) PRIMARY KEY

If MySQL then:

ID int NOT NULL AUTO_INCREMENT

Look at the following link: http://www.w3schools.com/sql/sql_autoincrement.asp

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