문제

My problem is that i have DataSet with Tables and i want to return values

'where DocumentType IN (@list)'

, but @list is like :

string.Join(",",DocTypes.List1);

I have tried to use it like SELECT........ WHERE DocumentType IN (@list) (in DataSet table adapter wizard), but when im executing Fill method, have ann error:

adapter1.Fill(ds.allDocuments, string.Join(",",DocTypes.List1);

Parameter 2 should be int, not string. Declare also wouldnt work in dataset..

Any help?

도움이 되었습니까?

해결책

You need a Split function, then this works:

WHERE
    ([DocumentType IN] IN
        (SELECT  Item
         FROM    dbo.Split(@list, ',') AS DocumentSplit)) 

This is a possible implementation of the table-valued function:

CREATE FUNCTION [dbo].[Split]
(
    @ItemList NVARCHAR(MAX), 
    @delimiter CHAR(1)
)
RETURNS @IDTable TABLE (Item VARCHAR(50))  
AS      

BEGIN    
    DECLARE @tempItemList NVARCHAR(MAX)
    SET @tempItemList = @ItemList

    DECLARE @i INT    
    DECLARE @Item NVARCHAR(4000)

    SET @tempItemList = REPLACE (@tempItemList, ' ', '')
    SET @i = CHARINDEX(@delimiter, @tempItemList)

    WHILE (LEN(@tempItemList) > 0)
    BEGIN
        IF @i = 0
            SET @Item = @tempItemList
        ELSE
            SET @Item = LEFT(@tempItemList, @i - 1)
        INSERT INTO @IDTable(Item) VALUES(@Item)
        IF @i = 0
            SET @tempItemList = ''
        ELSE
            SET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)
        SET @i = CHARINDEX(@delimiter, @tempItemList)
    END 
    RETURN
END  

Maybe you need to ensure manually in the parameters of the tableadapter that it's DbType is string:

enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top