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 参数发送?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01