简体   繁体   English

如何将int值列表从后面的代码传递给SP?

[英]How to pass List of int value to SP from code behind?

I have a List of int in that i am getting some value now I want to pass all the value to my store procedure one by one or comma separated, 我有一个int列表,因为我现在正在获取一些值,我想将所有值一一传递给我的存储过程,或者用逗号分隔,

Code Behind: 背后的代码:

Cmd.Parameters.AddWithValue("@ImageId", IDList); //IDList is list of int which need to pass in my SP

in SP 在SP中

@ImageId int //declaration

IF Not Exists(SELECT ImgIns.Id FROM ImgIns WHERE ImgIns.ImageId =@ImageId)

Passing them in a comma separated string and split it in t-sql is the best option I found so far. 到目前为止,我发现最好的选择是用逗号分隔的字符串传递它们并在t-sql中将其拆分。

Split function equivalent in T-SQL? 拆分功能是否等效于T-SQL? will help for the split operation. 将有助于拆分操作。

In Code Behind : 在后面的代码中:

var IDList = String.Join(",", new List<int> {1,2,3});
Cmd.Parameters.AddWithValue("@ImageIds", IDList);

Create a sql function to split the list of comma separated int http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648 insert this list into some table varibale and then you can wrtie your SP like 创建一个sql函数以逗号分隔的列表形式拆分int http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648将此列表插入到某些表变量中,然后可以像这样写成SP

DECLARE @ImageIDs TABLE (ImageID INT) 声明@ImageIDs表(ImageID INT)

INSERT INTO @ImageIDs select * from dbo.fn_Split(@ImageID) 将INSERT INTO @ImageIDs从dbo.fn_Split(@ImageID)中选择*

IF EXISTS (SELECT 1 FROM ImageINstance I INNER JOIN @ImageIDs Img ON I.ImageID = Img.ImageID) 如果存在(从ImageInstance中选择1,我在内部加入@ImageIDs Img on I.ImageID = Img.ImageID)

For now I am passing the value one by one with the help of for loop and its working fine for me.. 现在,在for循环的帮助下,我正在逐个传递值,它对我来说很好用。

 for (int i = 0; i < IDList.Count; i++)
 {
    Cmd.Parameters.Clear();
    Cmd.Parameters.AddWithValue("@ImageId", IDList[i]);
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM