Size 属性的大小无效为 0
我正在开发一个社交网络,我的一个程序返回 VARCHAR 输出。 这就是我写的:
SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar);
job1.Direction = ParameterDirection.Output;
但是出现了这个错误:
String[1]:Size 属性的大小无效,为 0。
I am working on a social network, one of my procedures returns a VARCHAR output.
So this is what I wrote:
SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar);
job1.Direction = ParameterDirection.Output;
However this error comes up:
String[1]: the Size property has an invalid size of 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
指定
varchar
参数时,您需要定义长度:您应该使用与 SQL Server 存储过程中定义的长度相同的长度。
顺便说一句:如果您的存储过程也没有定义长度(例如 @job VARCHAR OUTPUT ) - 那么您已经定义了 1 个字符长度的 varchar 字符串... ...
You need to define a length when specifying the
varchar
parameter:You should use the same length as defined in your SQL Server stored procedure.
And btw: if your stored procedure also has no length defined (something like
@job VARCHAR OUTPUT
) - then you've defined avarchar
string of 1 character length ......是的,必须定义
varchar
/nvarchar
数据类型的长度,如下所示。Yes, have to define the length for
varchar
/nvarchar
data type like below.如果您使用的是 Dapper,则您已为参数传递了一个空值,其中预期输入是一个字符串。
要查找有问题的值及其参数,请在调试会话期间检查 Dapper DynamicParameters 对象并打开索引引用的参数。
If you are using Dapper, you have passed in a null value for the parameter, where the expected input was a string.
To find the offending value and it's parameter, inspect at the Dapper DynamicParameters object during a debug session and open the parameters being index referred to.
在调用存储过程之前将空字符串分配给输出参数,这应该可以消除错误:
job1.Value = string.Empty;
Assign an empty string to the output parameter before calling the stored procedure and that should get rid of the error:
job1.Value = string.Empty;