c# - Exception in Add Parameter To DbCommand -


i need update command parameters, , reasons can't use stored procedures, generate update command depend on database, table , columns, following forms use:

        string constr = "provider=sqlncli10;server=.\\sql2008;database=mydatabase;trusted_connection=true;";          dbproviderfactory dbfactory = dbproviderfactories.getfactory("system.data.oledb");         dbconnection dbconnection = dbfactory.createconnection();         dbconnection.connectionstring = constr;          dbcommand dbcommand = dbfactory.createcommand();         dbcommand.commandtext = "update [student] set name = @name id = @id";          dbparameter param1 = dbcommand.createparameter();         param1.parametername = "@name";         param1.value = "lol";         dbparameter param2 = dbcommand.createparameter();         param2.parametername = "@id";         param2.value = 5;          dbcommand.parameters.add(param1);         dbcommand.parameters.add(param2);          dbconnection.open();         dbcommand.executenonquery();         dbconnection.close(); 

but there exception:

must declare scalar variable "@name"

where problem in code? have idea this?

as using system.data.oledb database provider ( regardless using sql server ) need use ? parameter placeholder like:

"update [student] set name = ? id = ?"; 

by using system.data.oledbprovider names of parameters doesn`t matter anymore need ensure occurance of parameters match order parameterobjects added command objects parameter collection.

edit: if want keep @ parameter placeholder can change this:

dbproviderfactory dbfactory = dbproviderfactories.getfactory("system.data.oledb"); 

to

dbproviderfactory dbfactory = dbproviderfactories.getfactory("system.data.sqlclient"); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -