Dapper DynamicParameters returning error

c# dapper oracle

Question

I am currently using the latest version of dapper, version 1.50.0-beta9. The provider I am using for Oracle is the Oracle.ManagedDataAccess, version 12.1.2400. I am getting an error when using dapper's dynamic parameters. My code is similar to the example below:

public class PersonDAL : OracleBase {

    public PersonDAL() : base() {
    }

    public PersonDAL(string connectionString) : base(connectionString) {
    }

    public const string ParamPersonID = ":personid";
    public const string ParamClassID = ":classid";
    private const string getPersonByClassAndID = "SELECT PERSON_ID AS PersonID, PERSON_DESCRIPTION AS PersonDescription, CLASS_ID AS ClassID FROM TABLE_PERSON WHERE PERSON_ID = " + ParamPersonID + " AND CLASS_ID = " + ParamClassID;

    public SystemModel GetModelByPersonID_ClassID(int classID, int personid) {
        DynamicParameter = new DynamicParameters();
        DynamicParameter.Add(ParamClassID, classID);
        DynamicParameter.Add(ParamPersonID, personid);
        return Connection.QuerySingle<SystemModel>(getPersonByClassAndID, DynamicParameter);
    }
}

This is returning this error:

Invalid parameter binding
Parameter name: personidAND

Expert Answer

You are missing syntax in the SQL. What you have is

WHERE Foo=:fooBar=:bar

You need a AND or OR and some spaces in the middle.

You don't actually need DynamicParameters in this scenario, btw. The "classic" dapper usage here would be just:

new { classID, personID } 

as the parameters object.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why