是否可以将 ID 集合作为 ADO.NET SQL 参数发送? [英] Is it possible to send a collection of ID's as a ADO.NET SQL parameter?

查看:16
本文介绍了是否可以将 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 参数发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆