c# - select query does not work with parameters using Parameters.AddWithValue -
the following query in c# doesn't work, can't see problem:
string getquery = "select * user_tbl emp_id=@emp_id , birthdate=@birthdate"; cmdr.parameters.addwithvalue("@emp_id", uservalidate.emp_id); cmdr.parameters.addwithvalue("@birthdate", uservalidate.birthdate); odbccommand cmdr = new odbccommand(getquery, conn); odbcdatareader reader = cmdr.executereader(); reader.hasrows returns no result when query database got data.
i'll assume code not quite presented, given wouldn't compile - you're using cmdr before declare it.
first, you're trying use named parameters, , according documentation of odbccommand.parameters, isn't supported:
when
commandtypesettext, .net framework data provider odbc not support passing named parameters sql statement or stored procedure calledodbccommand. in either of these cases, use question mark (?) placeholder.
additionally, avoid using addwithvalue anyway - use like:
string sql = "select * user_tbl emp_id = ? , birthdate = ?"; using (var connection = new odbcconnection(...)) { connection.open(); using (var command = new odbccommand(sql, connection)) { command.parameters.add("@emp_id", odbctype.int).value = uservalidate.employeeid; command.parameters.add("@birthdate", odbctype.date).value = uservalidate.birthdate; using (var reader = command.executereader()) { // use reader here } } } this example uses names following .net naming conventions, , demonstrates disposing of resources... fixing parameter issue.
i think it's unfortunate have provide name parameter when adding command though can't use in query, such life.
Comments
Post a Comment