c# - How to update a table using join in Visual Studio -


what's wrong?

sqlcommand cmd = new sqlcommand(@"update       perioada p join client c on p.id_client = c.id_client  set  p.date ='" + datetimepicker1.value.tostring("mm/dd/yyyy") + "' (c.cnp = '" + textbox1.text + "')", con); cmd.executenonquery(); 

enter image description here

assuming using sql server (because using system.data.sqlclient), below correct syntax update statement:

string sqlquery = "update p set  p.date =@dt perioada p join client c on p.id_client = c.id_client (c.cnp = @cnp)"  using (sqlconnection con = new sqlconnection(connectionstring)) {     sqlcommand command = new sqlcommand(sqlquery, con);     command.parameters.addwithvalue("@dt", datetimepicker1.value.tostring("mm/dd/yyyy"));     command.parameters.addwithvalue("@cnp", textbox1.text);      try     {         con.open();         command.executenonquery();     }     catch (exception ex)     {         console.writeline(ex.message);     } } 

on side note, should use sqlparameter pass input control's values sql server, instead of manually creating sql query appended values. way of creating query prone sql injection attack.

edit: edited answer depict way use parameterised query


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -