WHERE IN(ID 数组)
我有一个网络服务,它传递一个整数数组。 我想按如下方式执行 select 语句,但不断收到错误。 我需要将数组更改为字符串吗?
[WebMethod]
public MiniEvent[] getAdminEvents(int buildingID, DateTime startDate)
{
command.CommandText = @"SELECT id,
startDateTime, endDateTime From
tb_bookings WHERE buildingID IN
(@buildingIDs) AND startDateTime <=
@fromDate";
SqlParameter buildID = new SqlParameter("@buildingIDs", buildingIDs);
}
I have webservice which is passed an array of ints.
I'd like to do the select statement as follows but keep getting errors. Do I need to change the array to a string?
[WebMethod]
public MiniEvent[] getAdminEvents(int buildingID, DateTime startDate)
{
command.CommandText = @"SELECT id,
startDateTime, endDateTime From
tb_bookings WHERE buildingID IN
(@buildingIDs) AND startDateTime <=
@fromDate";
SqlParameter buildID = new SqlParameter("@buildingIDs", buildingIDs);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你不能(不幸的是)这样做。 Sql 参数只能是单个值,因此您必须这样做:
当然,这需要您知道有多少个构建 ID,或者动态构建查询。
作为解决方法*,我做了以下操作:
它将用数字替换语句的文本,最终结果如下:
You can't (unfortunately) do that. A Sql Parameter can only be a single value, so you'd have to do:
Which, of course, requires you to know how many building ids there are, or to dynamically construct the query.
As a workaround*, I've done the following:
which will replace the text of the statement with the numbers, ending up as something like:
首先,您需要一个函数和一个存储过程。 该函数将分割您的数据并返回一个表:
接下来您需要一个存储过程来使用它:
最后,您的代码:
这远远超出了您的问题所要求的范围,但它将满足您的需要。
注意:如果您传入任何不是 int 的值,整个数据库函数将会失败。 我将错误处理留给最终用户作为练习。
First you're going to need a function and a sproc. The function will split your data and return a table:
Next you need a sproc to use that:
Finally, your code:
That goes way beyond what your question asked but it will do what you need.
Note: should you pass in anything that's not an int, the whole database function will fail. I leave the error handling for that as an exercise for the end user.
注意:我一般不赞成使用非参数化查询。 然而,在这种情况下,考虑到我们正在处理一个整数数组,您可以这样做,而且效率会更高。 然而,考虑到每个人似乎都想降级答案,因为它不符合他们的有效建议标准,我将提交另一个执行得很糟糕但可能会在 LINK2SQL 中运行的答案。
假设,正如您的问题所述,您有一个整数数组,您可以使用以下代码返回一个字符串,该字符串将包含 SQL 接受的逗号分隔列表:
然后,我建议您跳过尝试参数化命令 < em>假设这是一个整数数组并且只需使用:
NOTE: I am not generally for using unparameterized queries. IN THIS INSTANCE, however, given that we are dealing with an integer array, you could do such a thing and it would be more efficient. However, given that everyone seems to want to downgrade the answer because it doesn't meet their criteria of valid advice, I will submit another answer that performs horribly but would probably run in LINK2SQL.
Assuming, as your question states, that you have an array of ints, you can use the following code to return a string that would contain a comma delimited list that SQL would accept:
Then, I would recommend you skip trying to parameterize the command given that this is an array of ints and just use:
超快的 XML 方法,不需要不安全的代码或用户定义的函数:
您可以使用存储过程并传递以逗号分隔的建筑物 ID 列表:
所有功劳都归于 Guru 布拉德·舒尔茨的博客
A superfast XML Method which requires no unsafe code or user defined functions :
You can use a stored procedure and pass the comma separated list of Building IDs :
All credit goes to Guru Brad Schulz's Blog
访问接受多个 Id 值的 T-SQL 存储过程 了解如何做到这一点的想法。
Visit T-SQL stored procedure that accepts multiple Id values for ideas on how to do this.
我使用这种方法并且对我有用。
我的变量 act = 我的 ID 列表,字符串。
I use that approach and works for me.
My variable act = my list of ID's at string.
也许我说得太详细了,但这个方法接受单个 int,而不是 int 数组。 如果您希望传入数组,则需要更新方法定义以拥有 int 数组。 获得该数组后,如果您计划在 SQL 查询中使用它,则需要将该数组转换为字符串。
Perhaps I'm being over detailed, but this method accepts a single int, not an array of ints. If you expect to pass in an array, you will need to update your method definition to have an int array. Once you get that array, you will need to convert the array to a string if you plan to use it in a SQL query.
你可以用这个。 在 SQLServer 中执行以在数据库上创建一个函数(仅一次):
创建函数后,您必须在代码中调用此函数:
此函数获取“array”上的文本内部逗号,并使用该值作为 int 创建一个表,称为ID。 当您的数据库中有此功能时,您可以在任何项目中使用。
感谢微软 MSDN。
Igo S Ventura
Microsoft MVA
Sistema Ari de Sá
[电子邮件受保护]
PS:我我来自巴西。 抱歉我的英语...XD
You can use this. Execute in SQLServer to create a function on your DB (Only once):
After create the function you have to call this on your code:
This function get the text inner commas on "array" and make an table with this values as int, called ID. When this function is on you DB you can use in any project.
Thanks to Microsoft MSDN.
Igo S Ventura
Microsoft MVA
Sistema Ari de Sá
[email protected]
P.S.: I'm from Brazil. Apologize my english... XD
这是我想到的一个 Linq 解决方案。 它会自动插入列表中的所有项目作为参数@item0、@item1、@item2、@item3等。
Here's a Linq solution I thought up. It'll automatically insert all items in the list as parameters @item0, @item1, @item2, @item3, etc.