来自 TextBox 的 ADO.NET 参数

发布于 2024-08-30 20:25:02 字数 563 浏览 3 评论 0原文

我试图在我的 C# Winforms 应用程序中从 SQL Server 2005 调用参数化存储过程。我从 TextBoxes 添加参数,如下所示(有 88 个):

cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar, 100).Value = CustomerName.Text;

我得到以下异常:

"System.InvalidCastException: Failed to convert parameter value
 from a TextBox to a String. ---> System.InvalidCastException:
 Object must implement IConvertible."

抛出错误的行是当我调用查询时:

cmd.ExecuteNonQuery();

我还尝试在 TextBoxes 上使用 .ToString() 方法,这似乎毫无意义无论如何,并抛出同样的错误。我传递的参数不正确吗?

I'm trying to call a parameterized stored procedure from SQL Server 2005 in my C# Winforms app. I add the parameters from TextBoxeslike so (there are 88 of them):

cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar, 100).Value = CustomerName.Text;

I get the following exception:

"System.InvalidCastException: Failed to convert parameter value
 from a TextBox to a String. ---> System.InvalidCastException:
 Object must implement IConvertible."

The line throwing the error is when I call the query:

cmd.ExecuteNonQuery();

I also tried using the .ToString() method on the TextBoxes, which seemed pointless anyway, and threw the same error. Am I passing the parameters incorrectly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鹿童谣 2024-09-06 20:25:02

您可能在分配参数值之一时忘记指定 Text 属性。

例如,而不是:

CustomerName.Text

您可能只做了:

CustomerName

如果有 88 个,这将很容易错过。

It's probable that you forgot to specify the Text property when assigning one of your parameter values.

For example, instead of:

CustomerName.Text

You may have done just:

CustomerName

This would be an easy thing to miss if there are 88 of them.

动听の歌 2024-09-06 20:25:02

我怀疑你在某个地方错过了 .Text,但你说你对所有参数值都做了 ToString,所以这看起来很奇怪。无论如何,您可以使用以下代码来转储参数名称和值的类型名称,这可能有助于发现有问题的参数。

foreach (SqlParameter param in cmd.Parameters)
{
  System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1}", param.ParameterName, param.Value == null ? "NULL" : param.Value.GetType().Name));
}

您应该在调试窗口中看到类似以下内容的内容,在本例中,CustomerSurname 是具有文本框而不是字符串的参数作为示例。

@CustomerName:String
@CustomerSurname:TextBox
...

I suspect you have missed a .Text somewhere, but you say you did a ToString on all the parameter values so that seems strange. Anyway, you can use the following piece of code to dump the parameter names and the type name of the value, this might help spot the problematic parameter.

foreach (SqlParameter param in cmd.Parameters)
{
  System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1}", param.ParameterName, param.Value == null ? "NULL" : param.Value.GetType().Name));
}

You should get something like the following in the debug window, in this case CustomerSurname is the parameter that has a Textbox and not a string as an example.

@CustomerName:String
@CustomerSurname:TextBox
...
素年丶 2024-09-06 20:25:02

尝试这样的事情:

SqlParameter sqlParam= new SqlParameter();
sqlParam.ParameterName = "@customerName";
sqlParam.Direction = ParameterDirection.Input;
sqlParam.SqlDbType = SqlDbType.VarChar;
sqlParam.Size = 100;
sqlParam.Value = customername.Text;
cmd.Parameters.Add(sqlParam);

try something like this:

SqlParameter sqlParam= new SqlParameter();
sqlParam.ParameterName = "@customerName";
sqlParam.Direction = ParameterDirection.Input;
sqlParam.SqlDbType = SqlDbType.VarChar;
sqlParam.Size = 100;
sqlParam.Value = customername.Text;
cmd.Parameters.Add(sqlParam);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文