如何在SQL参数字符串数组传递到SQL IN子句 [英] How to pass string array in SQL parameter to IN clause in SQL

查看:204
本文介绍了如何在SQL参数字符串数组传递到SQL IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在一个复杂的方式在做逻辑。

我只需要在一个存储过程来执行这个查询:

 选择尺寸,SUM(数量)
从tbl_SizeBreakup
其中(品牌= @品牌)
  和(Combo在('1','2')).​​..

我必须使用C#中的SQL参数中传递的组合是

 的DataSet DT =新的DataSet();
CMD =新的SqlCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText =sp_Accessories;
cmd.Connection = CON;cmd.Parameters.AddRange(
    新的SqlParameter [] {
        新的SqlParameter(@模式,模式)
        新的SqlParameter(@二合一,COMBO),
    }}

所以,如果我通过1参数,如预期工作。我应该通过该组合是一个的String [] (字符串数组)。数组的长度可以是任何东西依赖于UI用户选择。

我的问题是,如何通过的String [] 新的SqlParameter(@二合一,COMBO)

我的存储过程。

  ALTER PROC [DBO]。[sp_Accessories]

@mode VARCHAR(50)= NULL,
@id INT = NULL,
@brand VARCHAR(50)= NULL,
@department VARCHAR(MAX)= NULL,
@season VARCHAR(50)= NULL,
@groupname VARCHAR(MAX)= NULL,
@styles VARCHAR(50)= NULL,
@combo VARCHAR(50)= NULL,
@combo_color为nvarchar(MAX)= NULL,


如果@模式='getsizewise
开始
选择tbl_SizeBreakup其中(品牌= @品牌)和(部门= @部门)和(季= @赛季)和(GROUP_NAME = @组名)和大小,SUM(数量)(风格= @样式)
和(Combo_Color = @色)和(Total_Add_Qty ='总量')集团通过尺寸
结束


解决方案

简介的:尽管OP已经接受一个答案,我认为这将是更好地分享我的经验,因为我相信了方法我要展示的是更好然后一个接受的。

我发现,通过阵列到SQL Server数据库的最佳方法是使用用户定义的表类型和c#数据表
在你的情况,因为你想通过一维的字符串数组,这也很容易:

首先,你需要在数据库中创建用户定义的表类型:

  CREATE TYPE dbo.StringArray如表(
    StringItem的VARCHAR(50) - 您可以使用适合您需要的任何长度

然后,你需要在你的C#code创建一个DataTable:

  DataTable的DT =新的DataTable();
dt.Columns.Add(StringItem的的typeof(System.String));

然后改变你的存储过程来接受这种数据类型作为参数:

  ALTER PROC [DBO]。[sp_Accessories]

@mode VARCHAR(50)= NULL,
@id INT = NULL,
@brand VARCHAR(50)= NULL,
@department VARCHAR(MAX)= NULL,
@season VARCHAR(50)= NULL,
@groupname VARCHAR(MAX)= NULL,
@styles VARCHAR(50)= NULL,
@combo dbo.StringArray只读= NULL, - 注意此更改
@combo_color为nvarchar(MAX)= NULL,


如果@模式='getsizewise
开始
选择tbl_SizeBreakup其中(品牌= @品牌)和大小,SUM(数量)
(部门= @部门)和(季= @赛季)和(GROUP_NAME = @组名)和(样式= @样式)
和(Combo_Color = @色)和(Total_Add_Qty ='总量')
在(选择@Combo的StringItem)comboColumn - 注意此更改
集团通过大小
结束

然后,你需要将字符串数组转换为一个DataTable在C#code。

 的foreach(字符串s在YourStringArray){
    字符串[] TEMP = {S};
    dt.Rows.Add(临时);
}

添加数据表作为参数传递给存储过程:

  System.Data.SqlClient.SqlParameter SP =新Data.SqlClient.SqlParameter();
sp.SqlDbType = SqlDbType.Structured;
sp.Value = DT;
sp.ParameterName =@Combo;
cmd.Parameters.Add(SP);

生成并运行。

此方法应具有更好的性能,然后使用SQL用户定义的功能,并且也可以用于不同类型的数据。这是使用它的最佳理由之一:
想想,你需要传递的日期数组的情景:CSV方式需要SQL每个字符串转换为日期,而使用这种方法,你可以简单地传递日期原样,没有它们转换为字符串,然后返回日期。此外,您还可以通过2维数组或字典,你所要做的就是创建一个在您的SQL数据库适当的用户定义的数据类型。

注意:code直接写在这里,可能有一些错别字

A logic which I am doing in a complex way.

I just need to execute this query in a stored procedure:

select Sizes, SUM(Quantity)
from tbl_SizeBreakup
where (Brand=@brand)
  and (Combo in ('1','2')) ...

The combo I must pass in SQL parameter using in C# is

DataSet dt = new DataSet();
cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "sp_Accessories";
cmd.Connection = con;

cmd.Parameters.AddRange(
    new SqlParameter[] {
        new SqlParameter("@Mode",mode),
        new SqlParameter("@Combo",combo),
    }}

So if I pass 1 parameter, working as expected. The combo which I should pass is a string[] (string array). The array length can be anything depends on the user selecting in UI.

My question is, how to pass string[] to new SqlParameter("@Combo",combo)?

My stored procedure..

ALTER proc [dbo].[sp_Accessories]
(  
@Mode varchar(50)=null,
@id int=null,
@brand varchar(50)=null,
@department varchar(MAX)=null,
@season varchar(50)=null,
@groupname varchar(MAX)=null,
@styles varchar(50)=null,
@combo varchar(50)=null,
@combo_color nvarchar(max)=null,
)
as
if @Mode='getsizewise'
begin
select Sizes,SUM(Quantity) from tbl_SizeBreakup where (Brand=@brand) and (Department=@department) and (Season=@season) and (Group_Name=@groupname) and (Style=@styles) 
and (Combo_Color=@color) and (Total_Add_Qty='Total Quantity') Group By Sizes
end

解决方案

Introduction: Even though the OP already accepted an answer, I thought it would be better to share my experience, because I belive the approach I'm about to show is better then the one accepted.

I find that the best way to pass Arrays to sql server database is using a user defined table type and c# DataTable. In your case, since you want to pass a string array of one dimension, it's fairly easy:

First you need to create a user defined table type in your database:

 CREATE TYPE dbo.StringArray As Table (
    StringItem varchar(50) -- you can use any length suited for your needs
)

Then you need to create a datatable in your c# code:

DataTable dt = new DataTable();
dt.Columns.Add("StringItem", typeof(System.String));

Then change your stored procedure to accept this data type as a parameter:

ALTER proc [dbo].[sp_Accessories]
(  
@Mode varchar(50)=null,
@id int=null,
@brand varchar(50)=null,
@department varchar(MAX)=null,
@season varchar(50)=null,
@groupname varchar(MAX)=null,
@styles varchar(50)=null,
@combo dbo.StringArray Readonly=null, -- NOTE THIS CHANGE
@combo_color nvarchar(max)=null,
)
as
if @Mode='getsizewise'
begin
select Sizes,SUM(Quantity) from tbl_SizeBreakup where (Brand=@brand) and
(Department=@department) and (Season=@season) and (Group_Name=@groupname) and (Style=@styles) 
and (Combo_Color=@color) and (Total_Add_Qty='Total Quantity') 
and comboColumn in(select StringItem from @Combo) -- NOTE THIS CHANGE
Group By Sizes
end

Then you need to convert the string array to a dataTable in your c# code.

foreach (string s in YourStringArray) {
    string[] temp = {s};
    dt.Rows.Add(temp);
}

Add the DataTable as a parameter to the stored procedure:

System.Data.SqlClient.SqlParameter sp = new Data.SqlClient.SqlParameter();
sp.SqlDbType = SqlDbType.Structured;
sp.Value = dt;
sp.ParameterName = "@Combo";
cmd.Parameters.Add(sp);

Build and run.

This approach should have better performance then using an sql user defined function, and also can be used for different data types. this is one of the best reasons to use it: Consider a scenario where you need to pass an array of Dates: the csv approach requires sql to convert each string to a date, while with this approach you can simply pass the dates as is, without converting them to strings and then back to dates.Also, you can pass 2 dimensions array or dictionaries, all you have to do is create the appropriate user defined data type in your sql database.

Note: code written directly here, there might be some typos.

这篇关于如何在SQL参数字符串数组传递到SQL IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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