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

查看:158
本文介绍了是否有可能发送的ID的集合作为一个ADO.NET SQL参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如。我可以这样写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

这篇关于是否有可能发送的ID的集合作为一个ADO.NET SQL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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