将int数组传递给存储过程

时间:2017-08-25 12:49:25

标签: c# sql-server stored-procedures

我试图将整数数组传递给具有用户定义表类型的存储过程。这是脚本,创建类型:

SET ANSI_NULLS ON
            SET QUOTED_IDENTIFIER ON 
            CREATE TYPE dbo.TaskTypes AS TABLE (TaskType int)

程序:

CREATE PROCEDURE [dbo].[RemoveTasks]
                                @TaskTypes dbo.TaskTypes READONLY,
                                @personId INT
                            AS
                            BEGIN

                                SET NOCOUNT ON;
                                    UPDATE dbo.Tasks SET IsDeleted = 1 WHERE PersonId = @personId AND TaskTypes IN (SELECT nc.TaskType FROM @TaskTypes as nc)
                                SET NOCOUNT OFF;
                            END;

调用程序的代码:

        var taskTypesDT = new DataTable();
        taskTypesDT.Columns.Add("TaskType");

        foreach (var taskId in taskTypes) // taskTypes is a List of enum type, I'm trying to pass their values
        {
            var row = feedTypesDT.NewRow();
            row["TaskType"] = (int)taskId;
            taskTypesDT.Rows.Add(row);
        }

        var personIdParam = new SqlParameter("@personId", personId);
        var taskTypesParam = new SqlParameter();
        taskTypesParam.ParameterName = "@TaskTypes";
        taskTypesParam.SqlDbType = SqlDbType.Structured;
        taskTypesParam.TypeName = "dbo.TaskTypes";
        taskTypesParam.Value = tskTypesDT;

        return dc.Database.ExecuteSqlCommandAsync("EXEC dbo.RemoveTasks @personId, @TaskTypes", personIdParam, taskTypesParam);

当它执行sql时我得到异常:"输入clash:int与TaskTypes"

不兼容

我做错了什么?

0 个答案:

没有答案