是否有可能发送的ID的集合作为一个ADO.NET SQL参数?有可能、作为一个、参数、ID

2023-09-02 20:43:17 作者:墨染青衣颜

例如。我可以这样写code:

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:

介绍新的T-SQL的可编程特性在SQL Server 2008