c# - How to pass parameters to an object query? -


i have object query in ms access (getuserlogin) execute following:

parameters prmusername text, prmpassword text;  select id, lastname, firstname, middlename tblusers username = [prmusername] , password = [prmpassword]; 

i have method in c# execute object query in ms access (getuserlogin).

public bool login(string username, string password) {     com = new odbccommand("exec getuserlogin", con);      com.commandtype = commandtype.storedprocedure;      com.parameters.add("prmusername", odbctype.text).value = username;     com.parameters.add("prmpassword", odbctype.text).value = password;      con.open();      rea = com.executereader(); //odbcexception goes here      if (rea.hasrows == true)         return true;     else         return false; } 

i'm getting odbcexception:

error [07002] [microsoft][odbc microsoft access driver] few parameters. expected 2.

your first problem query uses parameter names same corresponding field names. if try run query in access prompt "username" , "password" parameters , return rows in tblusers regardless of parameter values enter. because when access sql parser processes

... username = [username] , password = [password] 

... interprets [username] , [password] field names, not parameter names, , result same as

... true , true 

so, first thing need change parameter names. 1 relatively common convention use "prm" prefix parameter names, query be

parameters prmusername text, prmpassword text; select [id], [lastname], [firstname], [middlename] [tblusers] [username] = [prmusername] , [password] = [prmpassword]; 

now, pass parameter values in c# application need use system.data.oledb (not .odbc) code this

using (var con = new oledbconnection(myconnectionstring)) {     con.open();     using (var cmd = new oledbcommand())     {         cmd.connection = con;         cmd.commandtype = commandtype.storedprocedure;         cmd.commandtext = "loginquery";         cmd.parameters.add("prmusername", oledbtype.varwchar).value = "eric";         cmd.parameters.add("prmpassword", oledbtype.varwchar).value = "abcdefg";         using (oledbdatareader rdr = cmd.executereader())         {             if (rdr.read())             {                 console.writeline("row found: id = {0}", rdr["id"]);             }             else             {                 console.writeline("row not found.");             }         }     } } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -