将tiny int转换为int时出错 [英] Error casting tiny int to int

查看:149
本文介绍了将tiny int转换为int时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此错误似乎是由于在服务器上安装框架4.5而引起的,即使该项目仍以4.0为目标。

This error looks like it was caused by installing framework 4.5 on the server even though the project is still targeted to 4.0.

4.5替换了CLR,看起来像它在将tinyint类型的对象拆箱为int时具有更改。

4.5 replaces the CLR and it looks like it has changes in unboxing an object of type tinyint to a int. This was working in 4.0 but not after installing 4.5.

========================= ===================

============================================

请在回答之前先阅读整个问题,大多数最新答案都不会回答我问的问题。

Please read the whole question before answering, most current answers do not answer the question I am asking.

我们今天在使用SQL的tinyint到使用int的int的转换中遇到了错误

We today got an error in a cast from a tinyint in sql to an int using

Daterow datarow = GetOneDatarow(connection,
                         "SELECT tinyintcolumn FROM table1 WHERE id = 4");
int i = (int)datarow["tinyintcolumn"];

这是旧的代码,已经存在于产品中多年,没有任何更改,并且一直在工作直到昨天。 (这不是确切的代码,仅足以显示上下文)

This is old code that has been in the product for several years without any changes and it has been working up until yesterday. (And it is not the exact code, just enough to show the context)

=== UPDATE

=== UPDATE

确切的错误消息是:指定的强制转换无效!最后一行

The exact error message was: "The specified cast is not valid!" and the last line

int i = (int)datarow["tinyintcolumn"];

是我们代码中强制转换错误的确切行,仅更改了变量名和列名。

is the exact row from our code casting the error with only variable names and column name changed.

并且数据库列为tinyint,默认值为0,没有索引或其他约束。

And the database column was tinyint with default value 0, no indexes or other constraints.

===结束更新

===更新2

汉克·霍尔特曼(Henk Holterman)在回答中告诉我,FW 4.5取代了4.0的CLR即使对于专门针对4.0编译的项目,这也可以像这样远程更改现有的4.0行为。

Henk Holterman in his response informed me that FW 4.5 replaces the CLR of 4.0 even for projects compiled specifically for 4.0 and this could remotely change existing 4.0 behaviour just like this.

我会再开放一段时间,但他的回答是最有希望的,因此far:D
=== End

I will keep this open a while more but his answer is the most promising so far :D === End

几周前我们从框架3.5更改为4.0,但是直到昨天下午,重新编译才发生,昨天早上,相同的代码(即使在重新编译后)也像发条一样工作。

We changed from framework 3.5 to 4.0 a few weeks ago but it was only yesterday afternoon after a recompile that this happened, yesterday morning the same code (even after recompile) worked like clockwork.

有人对为什么以前能工作但现在不工作有任何想法吗?

Do anyone have any idea on why this was working before and is not working now?

Microsoft是否做了任何修改引擎盖更改是否删除了隐式转换或以前是否曾由纯魔术完成过?

Has Microsoft made any under the hood changes that removed an implicit conversion or has it worked by pure magic before?

我们通过将数据库列更改为int来解决它,但我仍然对此感到好奇

We solved it by changing the database column to int, but I am still curious on what might have caused it to fail right now.

===更新3

只需完成此操作即可。

Just to complete this.

我发现了框架之间的变化。 Microsoft在一个更新中更改了装箱和拆箱的方式。这导致从字节到int的隐式转换,即当字节装箱时,较旧的FW确实失败,因为它在数据表中。

I found the change between frameworks. In an update Microsoft changed how boxing and unboxing is done. This caused the implicit cast from byte to int that older FW did to fail when the byte was boxed, as it is in a datatable.

未装箱的字节将在4.5

An unboxed byte will will in 4.5 be implicitly cast to an int, but a boxed byte is an generic object which cannot be implicitly cast.

不,这在3.5 SP1中已更改,因此我们的FW 4.0也应失败,除非对SP1的更新不在4.0更新中。这有待解决:)

Not, this was changed in 3.5 SP1 so our FW 4.0 should also have failed, unless the update to SP1 was not in the 4.0 update. This has yet to be answered :)

这里是MS的票证;)
https://connect.microsoft.com/VisualStudio/feedback/详细信息/ 766887 / casting-tinyint-from-sql-datarow-to-int-no-longer-possible

Here is the ticket from MS on this ;) https://connect.microsoft.com/VisualStudio/feedback/details/766887/casting-tinyint-from-sql-datarow-to-int-no-longer-possible

推荐答案

它永远不应该起作用。这使得该框架中的某些内容很可能已修复。

It should never have worked. Which makes it likely that something was fixed in the framework.

C#中的问题:

byte b = 3;       // TinyInt is a Byte
object o = b;     // DataRow[i] is an object
int i = (int)o;   // invalid cast

解决方法:

int i = (byte)datarow["tinyintcolumn"];






并从下面的评论路径中:


And from the comment trail below:


我们已经安装了它,但是该项目没有朝着4.5编译,只有朝着4.0编译,……是这样吗?

We have installed it, but this project is not compiled towards 4.5, only to 4.0, ... could that be the case?






是的,框架4.5取代了4.0的一部分。

Yes, framework 4.5 replaces parts of 4.0.

这篇关于将tiny int转换为int时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆