adrasteia adrasteia - 1 year ago 64
MySQL Question

C# Mysql create new row but if exist don't

I have a mysql table and my c# program includes something simular like login/register.

I am using this methods for openning connection, close it and some more stuff :

public void Insert(string id,string ip)
string query = "INSERT INTO skinsdb (id, acces, ip) VALUES('" + id + "', 1 , '" + ip + "')";

//open connection
if (this.OpenConnection() == true)
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);

//Execute command

//close connection
else MessageBox.Show("code 18");

I am simply using this for new row BUT i want if "id" already exist, just increase 1 acces(int) cell of his row. Thank you.

Answer Source

If you have just a single unique index on id then you can use

"INSERT INTO skinsdb (id, acces, ip) VALUES('" + id + "',  1 , '" + ip + "') 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download