Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert auto increment

Tags:

c#

mysql

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?

like image 669
Ren Avatar asked Dec 12 '22 07:12

Ren


1 Answers

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.

like image 69
har07 Avatar answered Dec 23 '22 21:12

har07