C# MySql.Data INSERT INTO error -


i use mysql.data in c# mysql connection. on program worked i'm hanging on insert command.

i following error:

an unhandled exception of type 'mysql.data.mysqlclient.mysqlexception' occurred in mysql.data.dll additional information: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'key) values ('pgjwzbpowtrputky')' @ line 1 

with code:

mysqlcommand command = connection.createcommand();         mysqldatareader reader;         command.commandtext = "insert jt_teamsync (key) values ('" + teamsynckey + "')";         connection.open();         reader = command.executereader();         connection.close(); 

thanks help

key reserved keyword in mysql. should escaped using backtick,

insert jt_teamsync (`key`) values(...) 

as sidenote, query weak. vulnerable sql injection. parameterized value avoid it, eg

string content = teamsynckey; string connstr = "connection string here"; string sqlstatement = "insert jt_teamsync (`key`) values (@key)"; using (mysqlconnection conn = new mysqlconnection(connstr)) {     using(mysqlcommand comm = new mysqlcommand())     {         comm.connection = conn;         comm.commandtext = sqlstatement;         comm.commandtype = commandtype.text;          comm.parameters.addwithvalue("@key", content);          try         {             conn.open();             comm.executenonquery();         }         catch(mysqlexception e)         {             // exception             // not hide             // e.message.tostring()         }     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -