将 CHAR(1) SQL 列转换为“char"时出错在代码中 [英] Error casting CHAR(1) SQL column as "char" in code
问题描述
我从名为 Combo_Label
的 SQL Server 2000 框 CHAR(1)
列中进行选择 - 它始终只有一个 A
..Z
字符在那里.在测试过程中,它会转换前 70 个左右的项目没有问题,但随后会遇到 Invalid Cast Exception
.
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. During testing, it will convert the first 70 or so items with no problem, but then runs into a Invalid Cast Exception
.
这是问题所在:
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?
推荐答案
数据库和 Db 访问 API 没有 char
的概念.您的 CHAR(1)
映射到 string
.
The database and the Db access APIs have no concept of char
. Your CHAR(1)
is mapped to string
.
可能是最明智的选择:
string comboLabel = (string)formCombo.Rows[j]["Combo_Label"];
但是如果你真的想要一个 char
:
but if you really want a char
:
char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];
这篇关于将 CHAR(1) SQL 列转换为“char"时出错在代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!