gpt4 book ai didi

c# - 使用 Informix 和 Dapper 向查询添加参数失败并出现语法异常

转载 作者:行者123 更新时间:2023-12-03 03:59:09 33 4
gpt4 key购买 nike

我尝试通过 ODBC 对 Informix 使用参数化查询,但任何添加参数的尝试都会失败,并出现以下异常:

$exception {"ERROR [42000] [Informix .NET provider][Informix]A syntax error has occurred."} System.Exception {IBM.Data.Informix.IfxException}

这是失败的代码:

List<ItemAttribute> items = con.Query<ItemAttribute>("select * from oe_cnvwrk where cwr_genero = @cwr_genero", new{cwr_genero = cwr_genero}).ToList();

使用不带参数的方法(如本例所示)可以完美运行,但会使应用程序面临注入(inject)攻击:

ItemHeader itemHeader = con.Query<ItemHeader>("select * from oe_cnvhdr where hdr_control_id = " + hdr_control_id).Single();

我可以在这里找到之前列出的关于此完全相同问题的问题,但从未得到解答。我希望有人知道如何处理这个问题:Dapper not adding parameters

有什么想法可以解决这个问题,或者是否有其他方法可以使用 Dapper 处理参数化?

最佳答案

尚未发布到 NuGet,但 source code现在包含对伪位置参数的支持。这些是通过模式识别实现的,例如文本中的 ?abc? 映射到成员 abc 命名的参数,但使用位置 ? 语法。因此,如果您发出:

List<ItemAttribute> items = con.Query<ItemAttribute>(
"select * from oe_cnvwrk where cwr_genero = ?cwr_genero?",
new{cwr_genero = cwr_genero}).ToList();

它应该可以工作;执行的实际查询是:

select * from oe_cnvwrk where cwr_genero = ?

其中添加的参数是来自标记为cwr_genero的成员的参数。这使得该成员能够被正确解析。特殊的 ?foo? 模式用作切换到位置参数的指示符。

请注意,每个查询只能引用单个参数一次;以下内容将不起作用:

select * from sometable where foo = ?x? or bar = ?x?

关于c# - 使用 Informix 和 Dapper 向查询添加参数失败并出现语法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25849759/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com