对象在将数组传递给存储过程时必须实现可恢复的错误

本文关键字:实现 可恢复 错误 存储过程 数组 对象 | 更新日期: 2023-09-27 18:32:48

我收到此错误:

对象必须实现 IConvertible。
第 225 行:SqlDataReader r = cmd.ExecuteReader();

SQL 服务器类型定义:

CREATE TYPE [dbo].[IdList] AS TABLE(
    [id] [int] NULL
)
GO

存储过程:

ALTER proc [dbo].[GetDictionaryWords2]
    @categories AS dbo.IdList READONLY
AS
select ... 
from ...
where [category] in (select id from @categories)

ASP.Net:

public static DictionaryWord[] GetDictionaryWords2(int[] categories)
{
                cmd.CommandText = "GetDictionaryWords2";
                var categoriesParams = cmd.Parameters.AddWithValue("@categories", categories);
                categoriesParams.SqlDbType = SqlDbType.Structured;
                categoriesParams.TypeName = "dbo.IdList";
                SqlDataReader r = cmd.ExecuteReader();
}

有什么想法吗?

对象在将数组传递给存储过程时必须实现可恢复的错误

您需要

将其作为DataTable而不是数组传递。从您发布的代码中,您可以将传递的数组转换为数据表并使用它

public static DictionaryWord[] GetDictionaryWords2(int[] categories)
{
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("ID");
            dt.Columns.Add(dc);
            categories.ToList().ForEach(dr => { DataRow d = dt.NewRow(); d[dc] = dr; dt.Rows.Add(d); });
                cmd.CommandText = "GetDictionaryWords2";
                var categoriesParams = cmd.Parameters.AddWithValue("@categories", dt);
                categoriesParams.SqlDbType = SqlDbType.Structured;
                categoriesParams.TypeName = "dbo.IdList";
                SqlDataReader r = cmd.ExecuteReader();
}