传递数组作为参数,存储过程不会更新结果

时间:2013-08-07 01:12:58

标签: asp.net-mvc stored-procedures

我有一个int []并将其作为参数传递给过程,当调用该方法时,不会报告错误,但数据库中没有更新任何内容。我的Add()可能有问题,因为存储过程是由另一个程序员编写的。我无法修改此存储过程,因此必须在Add()。

中解决所有问题

仅供参考,Add()将一组userID和一个groupID作为参数,格式化它们并运行存储过程。存储过程将userID和groupID插入到DB

For example:
if userIDs=[1,2,3] and groupID=4, 
then I want the following data to be inserted into the DB
userID    groupID
1            4
2            4
3            4 

存储过程

USE [xyz]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
        ALTER PROCEDURE [dbo].[spSaveSomething] 

            @groupID INT, 
            @userIDs TEXT
        AS
        BEGIN

            SET NOCOUNT ON;

            DECLARE @handle INT
            EXEC sp_xml_preparedocument @handle OUTPUT, @userIDs

            DELETE FROM tbl1 WHERE i_GroupID = @groupID AND i_NetworkUserID IN (SELECT [ID] FROM OPENXML (@handle, 'ROOT/VAL', 1) WITH ([ID] INT))

            INSERT INTO tbl1 (i_NetworkUserID, i_GroupID)
            SELECT [ID], @groupID FROM OPENXML (@handle, 'ROOT/VAL', 1) WITH ([ID] INT)

            EXEC sp_xml_removedocument @handle
        END

添加()

public void AddUsers(int[] UserIDs, int GroupID)
        {
                List<int> testList = new List<int>();

                foreach (int id in UserIDs)
                {
                    testList.Add(id);
                }

                XmlSerializer xs = new XmlSerializer(typeof(List<int>));
                MemoryStream ms = new MemoryStream();
                xs.Serialize(ms, testList);

                string resultXML = UTF8Encoding.UTF8.GetString(ms.ToArray());
                SqlParameter param1 = new SqlParameter("@userIDs", resultXML);
                SqlParameter param2 = new SqlParameter("@groupID", GroupID);
                context.Database.ExecuteSqlCommand("spSaveSomething @groupID, @userIDs",
                    param2, param1);

        }

2 个答案:

答案 0 :(得分:1)

int[]转换为以逗号分隔的字符串,然后在存储过程中将in转换为

WHERE i_NetworkUserID in @UserIds

答案 1 :(得分:0)

对我来说,你的方法有点复杂。

我建议重写你的程序,如

alter procedure [dbo].[spSaveSomething] 
(
    @groupID int, 
    @userIDs nvarchar(max)
)
as
begin
    set nocount on

    declare @tmp_Users table (ID int primary key)

    insert into @tmp_Users
    select T.C.value('.', 'int')
    from @userIDs.nodes('List/ID') as T(C)

    delete from tbl1
    where
        i_GroupID = @group_ID and
        i_NetworkUserID in (select ID from @tmp_Users)

    insert into tbl1 (i_NetworkUserID, i_GroupID)
    select ID, @groupID
    from @tmp_Users

    -- actually it's strange - you deleting users and then inserting
    -- may be you wanted to do this:

    -- delete from tbl1
    -- where
    --    i_GroupID = @group_ID and
    --    i_NetworkUserID not in (select ID from @tmp_Users)

    -- insert into tbl1 (i_NetworkUserID, i_GroupID)
    -- select U.ID, @groupID
    -- from @tmp_Users as U
    -- where
    --     not exists (
    --         select T.*
    --         from tbl1 as T
    --         where T.i_GroupID = @group_ID and T.i_NetworkUserID = U.ID
    --     )
end

和您的xml生成一样

var userIDs = new List<int>() { 1, 2, 3 };
var userXML = new XElement("List", a.Select(x => new XElement("ID", x))).ToString();
// so userXML = "<List><ID>1</ID><ID>2</ID><ID>3</ID></List>"

希望有所帮助