ADO.NET TableAdapter 参数

发布于 2024-07-15 07:25:49 字数 299 浏览 6 评论 0原文

我有一个查询,希望通过包含“IN”子句的 ASP.NET TableAdapter 运行,该子句通过参数接收其值。

我的问题是,如何指定这个参数? 我想这样编写条件语句:

AND b.group_category_id in (@ParamList)

其中@ParamList是参数的字符串,例如“4,12,45,23”,但由于指定的id是整数,它抱怨它无法将字符串转换为整数。 这是有道理的,但是有没有办法在 ASP.NET TableAdapter 的 SQL 语句中指定这样的列表?

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.

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, e.g. "4,12,45,23" but since the specified id is an Integer, it complains that it cannot convert String to Integer. This makes sense, but is there any way to specify such a list in a SQL statement in an ASP.NET TableAdapter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-07-22 07:25:49

您可以查看 http:// dotnet.org.za/johanvw/archive/2008/06/13/mssql-split-function.aspx 并将其确实作为字符串传递。 与其说是解决方案,不如说是一种解决方法。 但这只有在使用 MSSQL 时才可用。

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. Kind of a workaround than a solution. But that would only be usable if you use MSSQL.

一桥轻雨一伞开 2024-07-22 07:25:49

我见过的一种解决方法:

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

在这种情况下,@ParamList 将是一个格式为“,4,12,45,23”的字符串,

它并不“漂亮”,但保留了已编译查询的参数和优点。 由于您正在搜索数字,因此子字符串保证唯一匹配。

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,"

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.

彻夜缠绵 2024-07-22 07:25:49

您可以将 @ParamList 作为逗号分隔的字符串传递,解析该字符串并将结果插入到 #table 中,然后使用 IN 搜索 #table:

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

除此之外,您的选项是有限的,除非您想使用动态 SQL ( exec() 语句),但我建议您尽可能避免这种情况。

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:

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.

清风疏影 2024-07-22 07:25:49

回答我自己的问题:这是不可能的。

Answering my own question with: it can't be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文