如何正确的将字符串数组传给in

问题

C#使用join将数组的元素用逗号分隔为字符串传递给SQL Server存储过程要么报错,要么查询的表格结果为0条数据。

C#中的代码如下:

1
2
3
4
DataTable dataTable = DbHelperSQL.RunProcedure("proc_Data_GetUploadInfo", new IDataParameter[]
{
new SqlParameter("@BillCodes", $"'{string.Join("','", result.Select(x => x.prepare_num.ToString()).ToArray())}'"),
}, "get").Tables[0];

解决

经过各种尝试,我使用 exec sp_executesql @sql 这种方式解决的:

1
2
3
4
declare @sql nvarchar(max)
set @sql='select * from (select BillCode,BillType,NetWeight,Tare,TareWeightTime,GrossWeight,GrossWeightTime from OrderInfo union
select BillCode,BillType,NetWeight,Tare,TareWeightTime,GrossWeight,GrossWeightTime from EndInfo) as u where u.BillCode in('+@BillCodes+')'
exec sp_executesql @sql

如果你有更好的方法,请在评论区留言。

如何正确的将字符串数组传给in

https://ganzhixiong.com/p/5a8bb790/

Author

干志雄

Posted on

2019-05-13

Updated on

2019-05-13

Licensed under

Comments