database - SQL UPDATE Error in C# Console Application -
using ms visual studio 2010, ms access 2010(database converted .mdb) , program based on c# console application.
i'm new c# , on following:
1.i'm struggling following error:
syntax error in update statement.
i've tried can't seem find problem. i've looked @ possible error outside update statement still no luck. online not seem me problem , of regarding connection database server.
for (int w = 0; w <= 9; w++) { string pn = names[w]; int lvl = convert.toint32(level[w]), dmg = convert.toint32(damage[w]), plek = w+1; connectionstring = @"provider=microsoft.jet.oledb.4.0;" + @"data source=c:\users\home\desktop\game\game.mdb"; //tried 1 // querystring = "update scores set playername=\""+pn+"\", level=\"" +lvl+"\", damagereceived=\""+dmg+"\" pos="+ plek; //ended 1 querystring = "update scores set playername= @ppn, level= @plvl, damagereceived= @pdmg pos= @pplek"; using (oledbconnection connection = new oledbconnection(connectionstring)) using (oledbcommand command = new oledbcommand(querystring, connection)) { try { command.parameters.addwithvalue("@ppn", pn); command.parameters.add("@plvl", sqldbtype.int).value = lvl; command.parameters.add("@pdmg", sqldbtype.int).value = dmg; command.parameters.add("@pplek", sqldbtype.int).value = plek; connection.open(); /*command.parameters.addwithvalue("@pn", pn); command.parameters.addwithvalue("@lvl", lvl); command.parameters.addwithvalue("@dmg", dmg); command.parameters.addwithvalue("@plek", plek);*/ command.executenonquery(); } catch (exception ex) { console.writeline(ex.message); } } } input = console.readline();
i appreciate help. in advance.
level
reserved word in access sql. try using instead:
querystring = "update scores set playername= @ppn, [level]= @plvl, damagereceived= @pdmg pos= @pplek";
also, noted in answer, oledb ignores parameter names , pays attention order in appear in commandtext, people use question mark (?
) parameter placeholder/name, e.g.
querystring = "update scores set playername=?, [level]=?, damagereceived=? pos=?"; // ... command.parameters.addwithvalue("?", pn); command.parameters.add("?", sqldbtype.int).value = lvl; command.parameters.add("?", sqldbtype.int).value = dmg; command.parameters.add("?", sqldbtype.int).value = plek;
Comments
Post a Comment