Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
hi guys i want to check if text already exist in my table if exist don't insert and if doesn't exist insert it

i wrote command just to insert i don't know how to check before inserting

MySqlConnection conect = new MySqlConnection(connectionString);
                conect.Open();
                MySqlCommand cmsd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                lochwidTxt.Text = HWDI.GetMachineGuid();
                cmd.CommandText = "INSERT INTO userprof  (u_name, u_pass, u_ip) " + " Values ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + lochwidTxt.Text + "')";


What I have tried:

check if already exist if it doesn't exist insert it
Posted
Updated 12-May-17 14:22pm
Comments
[no name] 12-May-17 21:54pm    
Oh never mind my comment, You have already been told how to do this.

1 solution

Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 
Comments
Ilyas Zahir 12-May-17 21:28pm    
thanks for your information im using condition of 1 user for 1 one computer
[no name] 12-May-17 21:52pm    
So? Ever hear of doing things right the first time? Doesn't make sense to make users login to your program when they are already logged in. In any case, you write a query to select the user and if the result count is 1 or more then they already have a login.
Ilyas Zahir 12-May-17 22:24pm    
i think u didn't read carefuly what i want exactly :

is to check if exist and if it doesn't exist insert it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900