Size 属性的大小无效为 0

发布于 2024-12-22 22:53:42 字数 269 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤城病女 2024-12-29 22:53:42

指定 varchar 参数时,您需要定义长度

SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar, 50);

您应该使用与 SQL Server 存储过程中定义的长度相同的长度。

顺便说一句:如果您的存储过程也没有定义长度(例如 @job VARCHAR OUTPUT ) - 那么您已经定义了 1 个字符长度的 varchar 字符串... ...

You need to define a length when specifying the varchar parameter:

SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar, 50);

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 a varchar string of 1 character length ......

南…巷孤猫 2024-12-29 22:53:42

是的,必须定义 varchar / nvarchar 数据类型的长度,如下所示。

cmd.Parameters.Add("@Description", SqlDbType.VarChar, 150).Direction =
    ParameterDirection.Output;

Yes, have to define the length for varchar / nvarchar data type like below.

cmd.Parameters.Add("@Description", SqlDbType.VarChar, 150).Direction =
    ParameterDirection.Output;
装纯掩盖桑 2024-12-29 22:53:42

如果您使用的是 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.

enter image description here

眼泪也成诗 2024-12-29 22:53:42

在调用存储过程之前将空字符串分配给输出参数,这应该可以消除错误:

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文