Is it possible to send a collection of ID#39;s as a ADO.NET SQL parameter?(是否可以将 ID 集合作为 ADO.NET SQL 参数发送?)
问题描述
例如.我可以写这样的代码吗:
Eg. can I write something like this code:
public void InactiveCustomers(IEnumerable<Guid> customerIDs)
{
//...
myAdoCommand.CommandText =
"UPDATE Customer SET Active = 0 WHERE CustomerID in (@CustomerIDs)";
myAdoCommand.Parameters["@CustomerIDs"].Value = customerIDs;
//...
}
我知道的唯一方法是加入我的 IEnumerable,然后使用字符串连接来构建我的 SQL 字符串.
The only way I know is to Join my IEnumerable and then use string concatenation to build my SQL string.
推荐答案
通常你这样做的方法是传入一个逗号分隔的值列表,然后在你的存储过程中,解析出列表并将其插入一个临时表,然后您可以将其用于连接.从 Sql Server 2005 开始,这是处理需要保存数组的参数的标准做法.
Generally the way that you do this is to pass in a comma-separated list of values, and within your stored procedure, parse the list out and insert it into a temp table, which you can then use for joins. As of Sql Server 2005, this is standard practice for dealing with parameters that need to hold arrays.
这是一篇很好的文章,介绍了解决此问题的各种方法:
Here's a good article on various ways to deal with this problem:
将列表/数组传递给 SQL Server 存储过程
但是对于 Sql Server 2008,我们最终可以将表变量传递到过程中,首先将表定义为自定义类型.
But for Sql Server 2008, we finally get to pass table variables into procedures, by first defining the table as a custom type.
本文对此(以及 2008 年的更多功能)进行了很好的描述:
There is a good description of this (and more 2008 features) in this article:
SQL Server 2008 中新的 T-SQL 可编程功能简介
这篇关于是否可以将 ID 集合作为 ADO.NET SQL 参数发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以将 ID 集合作为 ADO.NET SQL 参数发送?


基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01