结合空单或空值的表值参数的存储过程(.NET)存储过程、参数、NET

2023-09-03 09:53:09 作者:纵有南风起

我创建了一个存储过程,将表值参数,它与int类型的单个列的表。这样做是为了简单地传递ID的列表进店过程并允许藻与数据工作。然而,在没有数据传递的情况下,我遇到的问题(事正常工作时,我有数据)。我转换列表到IEnumerable,并绑定表值参数的SP。我试图绑定一个空的列表,这就造成了误差

I have created a stored procedure that takes a table valued parameter that is a table with a single column of type int. The idea is to simply pass a list of ids into the store procedure and allow the sp to work with the data. However, in the case where there is no data to pass in, I am encountering problems (things work correctly when I have data). I am converting a List to an IEnumerable, and binding that to the table valued parameter for the sp. I have tried to bind an empty List, which resulted in the error

System.ArgumentException:有在的SqlDataRecord枚举没有记录。要发送表值参数没有记录,使用的值,而不是一个空引用。

System.ArgumentException: There are no records in the SqlDataRecord enumeration. To send a table-valued parameter with no rows, use a null reference for the value instead.

我又试图绑定一个空值(我认为是什么上面的消息是越来越AT),但只造成了不同的错误信息

I then tried to bind a null value (which I thought was what the above message was getting at), but that only resulted in a different error message

System.NotSupportedException:参数为DBNull值'@MainItemIdList'不支持。表值参数不能为DBNull。

System.NotSupportedException: DBNull value for parameter '@MainItemIdList' is not supported. Table-valued parameters cannot be DBNull.

它不会出现,你可以声明表值参数为可为空在SP声明。什么是正确的方法,空单结合于表值参数?

It does not appear that you can declare the table valued parameter as nullable in the sp declaration. What is the correct method for binding an empty list to at table valued parameter?

推荐答案

诀窍是:不传入参数都。为表值参数的默认值是一个空表。

The trick is: don’t pass in the parameter at all. The default value for a table-valued parameter is an empty table.

我意识到,这可能是为时已晚,以帮助你,但希望这将节省别人一些时间。异常消息是非常无益的。

I realise that this is probably far too late to help you, but hopefully it will save someone else some time. The exception message is extremely unhelpful.