c# - Why does it say there's an error in SQL statement Insert into -


i learning connect microsoft access , write database. created database in access , insert data it.

 oledbconnection conn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=c:\users\amin\desktop\namesdatabase1.accdb");   oledbcommand cmd = new oledbcommand();  string fn = textbox1.text;  string ln = textbox2.text;   cmd.commandtext = "insert names (firstname, lastname) values (fn,ln)";  cmd.connection = conn;   conn.open();  cmd.executenonquery(); 

before run there seems no error. run program, update textbox1.text , textbox2.text , press save, error:

syntax error in sql statement insert into

what did wrong??

in insert trying add values fn , ln. values not exists in command scope, not know tu it. want instead set values parameters , add them value command:

oledbconnection conn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=c:\users\amin\desktop\namesdatabase1.accdb"); oledbcommand cmd = new oledbcommand(); string fn = textbox1.text; string ln = textbox2.text; cmd.commandtext = "insert names (firstname, lastname) values (@fn,@ln)"; cmd.connection = conn; cmd.parameters.add(new oledbparameter("@fn", fn)); cmd.parameters.add(new oledbparameter("@ln", ln)); conn.open(); cmd.executenonquery(); 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -