[英]Error casting CHAR(1) SQL column as “char” in code
I'm selecting from a SQL Server 2000 box CHAR(1)
column called Combo_Label
- it will always have a single A
.. Z
character in there. 我从一个称为Combo_Label
的SQL Server 2000框CHAR(1)
列中进行Combo_Label
-它始终在其中具有单个A
.. Z
字符。 During testing, it will convert the first 70 or so items with no problem, but then runs into a Invalid Cast Exception
. 在测试期间,它将毫无问题地转换前70个左右的项目,但随后会变成Invalid Cast Exception
。
This is the problem line: 这是问题所在:
char comboLabel = (char)formCombo.Rows[j]["Combo_Label"];
This is a screenshot of the watch list showing some ways it can be evaluated. 这是监视列表的屏幕快照,显示了可以对其进行评估的一些方法。
Any thoughts as to why this occurs? 有什么想法为什么会发生这种情况?
The database and the Db access APIs have no concept of char
. 数据库和Db访问API没有char
概念。 Your CHAR(1)
is mapped to string
. 您的CHAR(1)
映射到string
。
probably the most sensible option: 可能是最明智的选择:
string comboLabel = (string)formCombo.Rows[j]["Combo_Label"];
but if you really want a char
: 但是,如果您真的想要一个char
:
char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];
该字段是一个字符串,您可以执行类似的操作,但还需要考虑hte字段是否为null或空字符串
char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];
首先将其转换为字符串 ,然后获取第一个字符...
char comboLabel = formCombo.Rows[j]["Combo_Label"].ToString()[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.