简体   繁体   English

如何在Dapper中为Oracle使用整数参数列表?

[英]How to use Parameter List of integers for Oracle with Dapper?

I am trying to re-write some code to use Dapper so I can easily use parameters. 我试图重新编写一些代码以使用Dapper,以便可以轻松使用参数。 I am trying to execute an UPDATE statement on an Oracle database. 我正在尝试在Oracle数据库上执行UPDATE语句。 A list of IDs to UPDATE is passed in as List<int> as parameter. 将要更新的IDs列表作为List<int>作为参数传递。 I want to update a field for each of the IDs passed in. The following is what I have: 我想为传入的每个IDs更新一个字段。以下是我所拥有的:

OracleConnection connection = ... // set earlier

public int IncreaseProcessCount(List<int> ids)
{
    var rowsAffected = connection.Execute(@"UPDATE TABLE SET PROCESSED_COUNT = PROCESSED_COUNT + 1 WHERE ID IN @ids", new { ids });
    return rowsAffected;
}

Before using Dapper, the execution statement was working just fine. 在使用Dapper之前,执行语句可以正常工作。 Now I am getting following error: 现在我得到以下错误:

ORA-00936: missing expression. ORA-00936:缺少表达式。

My current solution is based on below posts: 我当前的解决方案基于以下帖子:

Dapper query with list of parameters and Performing Inserts and Updates with Dapper 具有参数列表的Dapper查询 以及使用Dapper执行插入和更新

I am not sure if this is Oracle specific issue as I never worked with Oracle + Dapper combination. 我不确定这是否是Oracle特有的问题,因为我从未使用过Oracle + Dapper组合。 But I strongly suspect the way you are passing parameter is a problem. 但是我强烈怀疑您传递参数的方式是有问题的。 The exception "missing expression" is saying the same thing. 异常“缺少表达式”在说同样的话。

Refer modification of your code below: 请参阅下面的代码修改:

public int IncreaseProcessCount(int[] ids)
{
    var rowsAffected = connection.Execute(@"UPDATE TABLE SET PROCESSED_COUNT = PROCESSED_COUNT + 1 WHERE ID IN :ids", new { ids });
    return rowsAffected;
}

There are following differences: 有以下区别:

  1. Use of " :ids " instead of " @ids " . 使用“ :ids ”代替“ @ids I strongly suspect this is an issue because Oracle expects : instead of @ for parameters. 我强烈怀疑这是一个问题,因为Oracle期望使用:而不是@作为参数。
  2. Use of int[] instead of List<int> . 使用int[]代替List<int> This should not be an issue because Dapper supports IEnumerable for parameter list; 这应该不是问题,因为Dapper 支持 IEnumerable的参数列表。 so List should be OK. 所以List应该可以。 You have already tried this (as you mentioned in comments) without success. 您已经尝试了此操作(如您在评论中提到的那样),但没有成功。

Refer this question for Dapper with IN clause using Parameter List. 有关问题,请参阅使用参数列表的带有IN子句的Dapper。 Here is another resource. 这是另一种资源。

Edit (for comments): 编辑(评论):

The core of the problem was use of " :ids " which was correctly included in my answer. 问题的核心是使用“ :ids ”,它已正确包含在我的答案中。 I just corrected syntax error in the code above. 我只是更正了上面代码中的语法错误。

Also, I generally use DynamicParameters . 另外,我通常使用DynamicParameters Actually, it was not an issue in this case, so I removed that part which was present in first version of my answer. 实际上,在这种情况下这不是问题,因此我删除了我的答案第一版中存在的那部分。 Anyway, following is the code with DynamicParameters which should work equally. 无论如何,以下是带有DynamicParameters的代码,它们应该同样起作用。

public int IncreaseProcessCount(int[] ids)
{
    var param = new DynamicParameters();
    param.Add(":ids", ids);

    var rowsAffected = connection.Execute(@"UPDATE TABLE SET PROCESSED_COUNT = PROCESSED_COUNT + 1 WHERE ID IN :ids", param);
    return rowsAffected;
}

Based off Amit's answer, below is what I finally got to work. 根据阿米特(Amit)的回答,以下是我最后要做的工作。 I had to wrap the collection being passed in with an anonymous object. 我必须用匿名对象包装要传递的集合。

connection.Execute("UPDATE TABLE SET PROCESSED_COUNT = PROCESSED_COUNT+ 1 WHERE ID IN :ids",
                    new { ids });

I don't know Dapper (never even heard of it) so I apologize if this is a nonsense; 我不知道Dapper(甚至从未听说过),所以如果这是胡说八道,我深表歉意。 however, in this: 但是,在此:

WHERE ID IN @ids

IN suggests that Oracle expects a list of elements enclosed into parentheses. IN建议Oracle使用括号中的元素列表。 So, what is @ids really? 那么, @ids到底是什么? If it is a single value, parentheses aren't necessary. 如果是单个值,则不需要括号。 But, if there are two or more of them (though, up to 1000), they have to be separated by a comma and - as I said - enclosed into parentheses, such as (1, 2, 8) . 但是,如果有两个或更多(最多1000个),则必须用逗号分隔,并且-如我所说-括在括号中,例如(1, 2, 8) I suppose you've already separated them - now try to add () and see what happens. 我想您已经分离了它们-现在尝试添加()看看会发生什么。

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

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