ExecuteNonQuery 方法上关于值转换不成功的异常

发布于 2024-08-14 17:20:56 字数 1006 浏览 3 评论 0原文

我正在使用 OleDbCommand.ExecuteNonQuery() 将数据插入数据库:

ObjCommand = New OleDbCommand
ObjCommand.CommandType = CommandType.StoredProcedure
ObjCommand.CommandText = StrSQL

ObjCommand.Parameters.Add("field1", OleDbType.VarChar).Value = <something1>
ObjCommand.Parameters.Add("field", OleDbType.VarChar).Value = <something2>
(...)
(...)    
ObjCommand.Parameters.Add("field50", OleDbType.VarChar).Value = <something50>

ObjCommand.Connection = GetDBConnection(StrConnectionString)
ObjCommand.Connection.Open()


<some integer> = ObjCommand.ExecuteNonQuery()

并且有一个转换异常仅显示在最后一行:

将数据类型 varchar 转换为smallint 时出错

我想知道这是否是正常行为,以及如何知道转换问题发生在哪里。

更新:

ObjCommand.Parameters.Add("@IdMunFatoGerador", OleDbType.VarChar).Value 
                = ObjNFe.idMunFatoGerador

我通过注释每一行并取消注释一些行找到了这一行,这一行给了我上述例外。

ObjNFe.idMunFatoGerador 是一个字符串,也给我“将数据类型 varchar 转换为smallint 时出错”

i am using OleDbCommand.ExecuteNonQuery() to insert data into the database:

ObjCommand = New OleDbCommand
ObjCommand.CommandType = CommandType.StoredProcedure
ObjCommand.CommandText = StrSQL

ObjCommand.Parameters.Add("field1", OleDbType.VarChar).Value = <something1>
ObjCommand.Parameters.Add("field", OleDbType.VarChar).Value = <something2>
(...)
(...)    
ObjCommand.Parameters.Add("field50", OleDbType.VarChar).Value = <something50>

ObjCommand.Connection = GetDBConnection(StrConnectionString)
ObjCommand.Connection.Open()


<some integer> = ObjCommand.ExecuteNonQuery()

And there is a conversion exception that only shows up in the last line:

error converting datatype varchar to smallint

I'd like to know if it's the normal behavior, and how do i know where the conversion problem occurs.

update:

ObjCommand.Parameters.Add("@IdMunFatoGerador", OleDbType.VarChar).Value 
                = ObjNFe.idMunFatoGerador

i've found this line through commenting every line and uncommenting some, this one gives me the above said exception.

ObjNFe.idMunFatoGerador is a string and gives me "error converting datatype varchar to smallint" too

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

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

发布评论

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

评论(5

那小子欠揍 2024-08-21 17:20:56

这意味着查询的参数之一的类型错误。也就是说,当您应该传递 smallint(c# 中的 short)时,您却传递了 varchar

如果没有存储过程的定义,我们就无法猜测它是哪一个。

That implies that one of the parameters of the query is of the wrong type. Namely you are passing varchar when you should be passing a smallint (short in c#).

Without the definition of the stored procedure there's no way we can guess which one it is..

鸠书 2024-08-21 17:20:56

您以 varChar 形式传递给存储过程的参数之一在存储过程中以smallint 形式键入。并且,在这种情况下,服务器无法将您传入的值隐式转换为整数值。查看存储过程的定义,lala 或 lulu 的类型为smallint。然后查看您发送的实际值......

One of the parameters you are pasing to the stored procedure as a varChar is typed in the stored procedure as an smallint. And, in this case the value you are passing in cannot be converted implicitly by the server to an integer value. Look at the stored proc definition, Either lala, or lulu is typed as an smallint. Then look at actual values you are sending it...

明明#如月 2024-08-21 17:20:56

如果您使用数据集设计器,它将为您生成所有内容,并且您将收到编译器错误而不是运行时错误。向您的项目添加一个新的数据集,然后向该数据集添加一个查询。

你最终会得到这样的结果:

QueriesTableAdapter ta = new QueriesTableAdapter();
ta.Connection = myConnection;
ta.MySeveralParameterStoredProc(x0, x1, ..., xN);

If you use the DataSet designer, it will generate everything for you and you'll get a compiler error instead of a run-time error. Add a new DataSet to your project then add a Query to the DataSet.

You end up with something like this:

QueriesTableAdapter ta = new QueriesTableAdapter();
ta.Connection = myConnection;
ta.MySeveralParameterStoredProc(x0, x1, ..., xN);
故事与诗 2024-08-21 17:20:56

我想您可以循环遍历参数集合并查看该值并查看它是否可以是数字(string.isnumeric)。使用 debug.assert 会输出一条消息,提示参数值太大,不能是小 int 以及参数名称。更好的是您将参数类型设置为 oledbtype.smallint,然后只查看这些。最终,您需要了解参数以及它们如何与底层 SQL 相对应。我只是通过正确输入参数来缩小搜索范围,然后确保我不会将任何不起作用的命令对象传递给命令对象。 HTH。
可能的代码:

For each parameter as SqlParameter in mycommandobject.parameters
     if isnumeric(parameter.value) then
          debug.assert(convert.int32(parameter.value) <= 32,767,"This parameter could have an issue - " & parameter.parametername & " value = " & parameter.value) 
     end if

loop

我还没有测试过代码,但我相信这会起作用。

I guess you could loop through the parameter collection and look at the value and see if it can be numberic (string.isnumeric). The use debug.assert to output a message that the parameter value is too big to be a small int as well as the parameter name. Even better is for you to set the parameter type to be oledbtype.smallint and then only look at those. Ultimately, you need to know your parameters and how they correspond to the underlying SQL. I would just narrow my search by typing my parameters correctly and then ensure I never passed anything to the command object that wouldn't work. HTH.
Possible code:

For each parameter as SqlParameter in mycommandobject.parameters
     if isnumeric(parameter.value) then
          debug.assert(convert.int32(parameter.value) <= 32,767,"This parameter could have an issue - " & parameter.parametername & " value = " & parameter.value) 
     end if

loop

I haven't tested the code, but i believe this will work.

盛夏尉蓝 2024-08-21 17:20:56

我终于找到了。

值的格式一切正常。
问题是:缺少一个参数。我仍然没有完全理解它,但问题是缺少的参数(smallint)在下面的一个(varchar)中被解释,所以我发现的错误是在第二个参数中。

换句话说,field~35 丢失了(哈哈)

所以问题是:当将命令安装到过程时,请记住始终以准确的数量和顺序放置字段。 =)

谢谢你们!

I've finally found it.

It was everything ok with the formats of the values.
The problem was: one of the parameters was missing. I still didn't understand it completely, but the issue was that the missing parameter (smallint) was interpreted in the following one (varchar) and so the error i found was in the second one.

In other words, field~35 was missing (haha)

So the thing is: when mounting a command to a procedure, remember to always put the fields in the exact amount and order. =)

Thank you guys!

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