简体   繁体   English

ADO.NET TableAdapter参数

[英]ADO.NET TableAdapter parameters

I have a query that I wish to run through an ASP.NET TableAdapter that contains an 'IN' clause which is to receive it's values through a parameter. 我有一个查询,我希望通过ASP.NET TableAdapter运行,该表包含一个'IN'子句,它通过参数接收它的值。

My question is, how do I specify this parameter? 我的问题是,如何指定此参数? I thought of writing the conditional statement like this: 我想写这样的条件语句:

AND b.group_category_id in (@ParamList)

where the @ParamList is a String of the parameters, eg "4,12,45,23" but since the specified id is an Integer, it complains that it cannot convert String to Integer. 其中@ParamList是参数的字符串,例如“4,12,45,23”,但由于指定的id是Integer,它会抱怨它无法将String转换为Integer。 This makes sense, but is there any way to specify such a list in a SQL statement in an ASP.NET TableAdapter? 这是有道理的,但有没有办法在ASP.NET TableAdapter的SQL语句中指定这样的列表?

You might have a look at http://dotnet.org.za/johanvw/archive/2008/06/13/mssql-split-function.aspx and pass it indeed as a string. 您可以查看http://dotnet.org.za/johanvw/archive/2008/06/13/mssql-split-function.aspx并将其确实作为字符串传递。 Kind of a workaround than a solution. 一种解决方法而不是解决方案。 But that would only be usable if you use MSSQL. 但是只有使用MSSQL才能使用它。

One workaround I've seen: 我见过的一个解决方法:

WHERE charindex(',' + cast(b.group_category_id as varchar) + ',', @ParamList) > 0

In this case the @ParamList would be a string in the form ",4,12,45,23," 在这种情况下,@ ParamList将是“,4,12,45,23”形式的字符串。

It's not "pretty", but preserves the parameter and benefits of a compiled query. 它不是“漂亮”,但保留了编译查询的参数和好处。 Since you are searching for numbers, the sub-string guarantees a unique match. 由于您要搜索数字,因此子字符串可确保唯一匹配。

回答我自己的问题:它无法完成。

You could pass @ParamList through as a comma-delimited string, parse the string and insert the results into a #table, then use IN to search the #table: 您可以将@ParamList作为逗号分隔的字符串传递,解析字符串并将结果插入到#table中,然后使用IN搜索#table:

AND b.group_category_id in (select group_category_id from #group_category_id_list)

Apart from this your options are limited, unless you want to use dynamic SQL (exec() statement), but I'd advise you to avoid that if possible. 除此之外,您的选项是有限的,除非您想使用动态SQL(exec()语句),但我建议您尽可能避免使用它。

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

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