database - Syntax error in INSERT STATEMENT from c# to Access DB -
con.open(); cmd = new oledbcommand("insert login (user, password) values ('" +textbox1 .text + "','" + textbox2 .text + "');",con); cmd.commandtype = commandtype.text; int temp = cmd.executenonquery(); if (temp > 0) { textbox1.text = null; textbox2.text = null; messagebox.show("record successfuly added"); } else { messagebox.show("record fail added"); } con.close();
when try insert of error appear ( syntax error in insert statement ) i'm try different method values parameters or direct plz !
- escape reserved keyword
user
- use parameterized query
- avoid sql injection
- make use of disposable objects
try approach:
using (oledbconnection con = new oledbconnection(connectionstring)) { con.open(); using (var cmd = new sqlcommand( "insert login ([user], [password]) values (@user, @pass);", con)) { cmd.parameters.add(new oledbparameter("@user", textbox1.text )); cmd.parameters.add(new oledbparameter("@pass", textbox1.text )); if (temp > 0) { textbox1.text = string.empty; textbox2.text = string.empty; messagebox.show("record successfuly added"); } else { messagebox.show("record fail added"); } } }
Comments
Post a Comment