简体   繁体   English

在Dapper中使用'?'

[英]using Dapper with '?'

I'm attempting to use Dapper to communicate with a DB2 server. 我正在尝试使用Dapper与DB2服务器进行通信。 The DB2 server doesn't support named parameters out of the box. DB2服务器不支持现成的命名参数。 It expects a question mark in the query for each parameter. 它期望查询中的每个参数都有一个问号。 Is there a way to support this with Dapper? Dapper有什么方法可以支持这一点吗? Maybe Dapper can replace all @Name stuff with ? 也许Dapper可以将所有@Name替换为? before calling the query? 在调用查询之前? And if so, does Dapper generate the parameters in order? 如果是这样,Dapper是否按顺序生成参数?

If not, it seems that the newer DB2 client drivers support named parameters, but it is off by default. 如果不是,则似乎较新的DB2客户端驱动程序支持命名参数,但默认情况下处于关闭状态。 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. 我尝试将该参数添加到客户机上的db2cli.ini [COMMON]部分,但行为没有变化。 That was for the OleDB driver. 那是针对OleDB驱动程序的。

Update: I then tried the .NET driver. 更新:然后,我尝试了.NET驱动程序。 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."} {“ ERROR [07004] [IBM] [DB2 / NT64] SQL0313N对于参数化游标,EXECUTE语句中的变量数,OPEN语句中的变量数或OPEN语句中的参数数不等于所需的值数。“}

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? INSERT算作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.) (我直接使用SqlMapper.cs ,并且可以在调试器中看到所有内容。)

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
            });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM