我尝试在 C# 和 SQL Server 2008 中使用用户定义的数据类型,但在执行 command.ExecuteReader() 时引发异常。
string qyery = "SELECT * FROM Product WHERE IsAvailableProduct < @Param";
SqlCommand command = new SqlCommand(qyery, conn);
SqlParameter param = new SqlParameter("@Param", SqlDbType.Structured);
param.UdtTypeName = "MyBolean";
param.SqlDbType = SqlDbType.Structured;
param.Value = 1;
command.Parameters.Add(param);
SqlDataReader reader = command.ExecuteReader();
引发的异常是:
无法将参数值从 Int32 转换为 IEnumerable``1。
我的代码有什么问题?
编辑:
如果我将SqlDbType更改为Udt,则会出现此异常:指定的类型未在目标服务器上注册。系统.Int32,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089。
如果我设置将 DbType 属性设置为 Int (这是“MyBolean”的基本类型),结果是异常无法将参数值从 Int32 转换为 IEnumerable1.`` 也被提出。
编辑:
我修改了标题,因为在 SQL Server 2008 中,它是“用户定义数据类型”而没有“用户定义类型”
I'm trying to use a User-Defined-Data Type with C# and SQL Server 2008 but I have an exception raised when command.ExecuteReader() is executed.
string qyery = "SELECT * FROM Product WHERE IsAvailableProduct < @Param";
SqlCommand command = new SqlCommand(qyery, conn);
SqlParameter param = new SqlParameter("@Param", SqlDbType.Structured);
param.UdtTypeName = "MyBolean";
param.SqlDbType = SqlDbType.Structured;
param.Value = 1;
command.Parameters.Add(param);
SqlDataReader reader = command.ExecuteReader();
The raised exception is :
Failed to convert parameter value from a Int32 to a IEnumerable``1.
What's wrong in my code ?
EDIT :
If I change the SqlDbType to Udt, I have this exception : Specified type is not registered on the target server.System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
If I set the DbType property to Int (which is the base type of "MyBolean") the result is the exception Failed to convert parameter value from a Int32 to a IEnumerable
1.`` is raised too.
EDIT :
I modified the title because in SQL Server 2008, it's "User Define DATA Type" and no "User Defined Types"
发布评论
评论(5)
SqlDbType
枚举值Structured
用于指示您正在传递 表值参数,这就是您收到错误消息的原因,因为您传入的值是一个整数,而不是用作的“某物列表” TVP。您需要根据
MyBolean
的基础数据类型设置param.SqlDbType
,我怀疑它可能是Bit
或Int
。The
SqlDbType
enumeration valueStructured
is used to indicate that you're passing around a Table Value Parameter, which is why you're getting the error message you're getting as the value you're passing in is an integer, not a "list of something" for use as a TVP.You need to set
param.SqlDbType
based on the underlying datatype forMyBolean
which I suspect is probablyBit
orInt
.乍一看,我猜你至少需要创建
SqlDbType = Udt
(不是 Stuctured)。At first glance I would guess you at least need to make
SqlDbType = Udt
(not Stuctured).这种类型必须存在于你的
数据库
SqlDbType=SqlDbType.结构化你的
类型“MyBoolean”必须是
“表值参数” 看看
这个和有关使用表值参数的更多信息
所以当您设置这些属性时,参数值必须至少实现 IEnumerable (即对象列表),但您提供整数值,这会导致错误
this type must exist in your
database
SqlDbType=SqlDbType.Structured your
type "MyBoolean" must be
"table-valued parameter" look at
this and more info about using table-valued parameters
so when you set those properies parameter value must be at least implement IEnumerable (i.e. a list of objects), but you provide integer value and this caused error
我的数据库中有用户定义的表类型(假设这是 MyTableTypeList)。这些在我的存储过程中用作参数。
要通过此数据类型传递值,我执行以下操作:
声明一个类似于我的用户定义表类型的 DataTable,并在其中填充值。
将 SqlParameter 属性 SqlDbType 指定为 SqlDbType.Structured "param.SqlDbType = SqlDbType.Structured"
将 TypeName 属性指定为用户定义的表类型的名称。 "param.TypeName = "MyTableTypeList"
I have User-Defined Table Types in my database (assume this to be MyTableTypeList). These are used as parameters in my stored procedure.
To pass values through this data type I do the following:
Declare a DataTable similar to my User-Defined Table Type, populate values in it.
Assign SqlParameter property SqlDbType as SqlDbType.Structured "param.SqlDbType = SqlDbType.Structured"
Assign TypeName property with the name of the User-Defnied Table Type. "param.TypeName = "MyTableTypeList"
指定的类型未在目标服务器上注册。System.Int32,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089
:恐怕,您不能使用原始类型,例如 <Udt
类型的Value
属性中的 code>int32 或string
范围。您应该在 .NET 中创建自定义 UDT 类(使用 SqlUserDefinedTypeAttribute)并在 SQL-Server 上注册它们(请参阅详细信息 此处),或者您可以“回退”以使用 UDT 所基于的基本 SQL 类型,例如Decimal
、DateTime
、 .NET 代码中的NVarChar
等,而不是Udt
。我遇到了同样的问题,并投票支持第二个选项。Specified type is not registered on the target server.System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
: I'm afraid, you cannot use primitive types, likeint32
orstring
in theValue
property of aUdt
type parameter. You should either create your custom UDT-Classes in .NET (decorating them with the SqlUserDefinedTypeAttribute) and register them on the SQL-Server (see the details here and here), or you can "fallback" to use the base SQL-types your UDTs based on, likeDecimal
,DateTime
,NVarChar
etc. in your .NET code instead ofUdt
. I faced the very same problem, and voted for the second option.