using Dapper with '?'

c# dapper db2

Question

I'm attempting to use Dapper to communicate with a DB2 server. The DB2 server doesn't support named parameters out of the box. It expects a question mark in the query for each parameter. Is there a way to support this with Dapper? Maybe Dapper can replace all @Name stuff with ? before calling the query? And if so, does Dapper generate the parameters in order?

If not, it seems that the newer DB2 client drivers support named parameters, but it is off by default. I can't figure out how to turn it on. I tried adding that parameter to the db2cli.ini [COMMON] section on my client with no change in behavior. That was for the OleDB driver.

Update: I then tried the .NET driver. That one seems to parse the variable names, but I still get a strange error when running:

{"ERROR [07004] [IBM][DB2/NT64] SQL0313N The number of variables in the EXECUTE statement, the number of variables in the OPEN statement, or the number of arguments in an OPEN statement for a parameterized cursor is not equal to the number of values required."}

My statement looks like this:

INSERT INTO XD.ALERT (PERFORMANCE_ID, CATEGORY, TITLE, DESCRIPTION, DATETIME) VALUES(1234, :Level, :AlertID, :AlertDesc, :DateTime)

Does an INSERT count as an EXECUTE? As far as I can tell I have four parameters in the query and four in the command object being used. (I'm using SqlMapper.cs directly and I can see everything in the debugger.)

Popular Answer

You can try this:

    public void SaveAlert(int? level, int? alertId, string alertDesc, DateTime date)
    {            
        _conn.Execute("INSERT INTO XD.ALERT(PERFORMANCE_ID, CATEGORY, TITLE, DESCRIPTION, DATETIME) VALUES(1234, @Level, @AlertID, @AlertDesc, @DateTime)",                
            new {
                Level = level,
                AlertId = alertId,
                AlertDesc = alertDesc,
                DateTime = date
            });
    }



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