我如何使用'在哪里'的小巧玲珑 [英] How do I use 'Where In' in Dapper

查看:205
本文介绍了我如何使用'在哪里'的小巧玲珑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试失败,现在一会儿就使用的IEnumerable<串> 其中 。子句中小巧玲珑



在文档,但它说,的IEnumerable< INT> 是支持在使用其中,但我甚至无法得到那个工作。



Dapper让您在IEnumerable的<通行; INT>并会自动参数化查询。



该错误消息我不断收到是一个SQL语法错误。 附近有语法错误','。



我已经把一些测试代码,我希望将证明什么,我我想要的目的。






 字符串CONNSTRING =服务器= *。*。 *;数据库= *;用户ID = *;密码= *;; 

串sqlStringIn = @选择StringText FROM
(选择1号,'A'的StringID,这是一个测试StringText
UNION SELECT 2 ID,'B'的StringID 另一个测试StringText
UNION选择3号,'C'的StringID,而另一位StringText
UNION选择4号,'D'的StringID,并再次StringText
UNION SELECT 5号,'E'的StringID,'再次'StringText)数据
,其中的StringID IN(@str);

串sqlIntegerIn = @选择StringText FROM
(选择1号,'A'的StringID,这是一个测试StringText
UNION SELECT 2 ID,'B'的StringID 另一个测试StringText
UNION选择3号,'C'的StringID,而另一位StringText
UNION选择4号,'D'的StringID,并再次StringText
UNION SELECT 5号,'E'的StringID,'再次'StringText)数据
,其中身份证件(@integer);使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{
conn.Open()


;

名单,LT; INT>整数=新列表与所述; INT> {1,2,3};
名单,LT;字符串>串=新的List<串GT; {A,B,C};

VAR参数= {新海峡=字符串,整数=整数};

//这里失败
IEnumerable的<串GT; intTest = conn.Query<串GT;(sqlIntegerIn,参数,命令类型:System.Data.CommandType.Text);

//这里
IEnumerable的<串GT; stringTest = conn.Query<串GT;(sqlStringIn,参数,命令类型:System.Data.CommandType.Text);

}


解决方案

要做到这里需要什么,短小精悍的需求来改变对飞SQL - 所以它需要的真正的肯定是做正确的事。常规有效的SQL语法包括括号:

 其中的StringID IN(@str)

要从这个歧义时,的巫术短小精悍的语法忽略的括号:

 ,其中的StringID在@str 

如果它检测到这一点,它会寻找一个名为参数 STR ,并将其扩展到之一:

 ,其中1 = 0  - 如果没有值
,其中的StringID = @str - 如果只有一个价值
,其中的StringID IN(@ STR0,@ STR1。 ..) - 如果超过一个价值

不过,短版:删除括号


I've been trying unsuccessfully now for a while to use an IEnumerable<string> with a WHERE IN clause in Dapper.

In the documentation, it does say that IEnumerable<int> is supported for use in a WHERE IN but I can't even get that to work.

Dapper allow you to pass in IEnumerable<int> and will automatically parameterize your query.

The error message I keep receiving is an Sql syntax error. Incorrect syntax near ','.

I've put together some test code that I hope will demonstrate what I am trying to achieve.


string connString = "Server=*.*.*.*;Database=*;User Id=*;Password=*;";

string sqlStringIn = @"SELECT StringText FROM 
                (SELECT 1 ID, 'A' StringID, 'This is a test' StringText
                UNION SELECT 2 ID, 'B' StringID, 'Another test' StringText
                UNION SELECT 3 ID, 'C' StringID, 'And another' StringText
                UNION SELECT 4 ID, 'D' StringID, 'and again' StringText
                UNION SELECT 5 ID, 'E' StringID, 'yet again' StringText) data
                WHERE StringId IN (@str)";

string sqlIntegerIn = @"SELECT StringText FROM 
                (SELECT 1 ID, 'A' StringID, 'This is a test' StringText
                UNION SELECT 2 ID, 'B' StringID, 'Another test' StringText
                UNION SELECT 3 ID, 'C' StringID, 'And another' StringText
                UNION SELECT 4 ID, 'D' StringID, 'and again' StringText
                UNION SELECT 5 ID, 'E' StringID, 'yet again' StringText) data
                WHERE ID IN (@integer)";


using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    List<int> integers = new List<int>{ 1, 2, 3 };
    List<string> strings = new List<string> { "A", "B", "C" };

    var parameters = new {str = strings, integer = integers };

    //fails here
    IEnumerable<string> intTest = conn.Query<string>(sqlIntegerIn, parameters, commandType: System.Data.CommandType.Text);

    //and here
    IEnumerable<string> stringTest = conn.Query<string>(sqlStringIn, parameters, commandType: System.Data.CommandType.Text);

}

解决方案

To do what is needed here, dapper needs to alter the SQL on the fly - so it needs to be really sure that it is doing the right thing. The regular valid SQL syntax includes parenthesis:

WHERE StringId IN (@str)

To disambiguate from this, the voodoo dapper syntax omits the parenthesis:

WHERE StringId IN @str

If it detects this, it looks for a parameter called str, and expands it, to one of:

WHERE 1=0 -- if no values
WHERE StringId = @str -- if exactly one value
WHERE StringId IN (@str0, @str1, ...) -- if more than one value

But short version: remove the parenthesis.

这篇关于我如何使用'在哪里'的小巧玲珑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆