简体   繁体   English

IF陈述式中的C#OdbcDataReader.GetBoolean InvalidCastException

[英]C# OdbcDataReader.GetBoolean InvalidCastException In IF-Statement

Please forgive my broken English first. 请原谅我的英语不好。

Well,antiduh's method of debug helps me to know what happen on my code,thanks everyone who join discuss. 好吧,感谢所有参与讨论的人,antiduh的调试方法可帮助我了解代码中发生的情况。


When I use OdbcDataReader.GetBoolean single,it's OK and no problem,just like below: 当我使用OdbcDataReader.GetBoolean single时,没问题,没问题,如下所示:

OdbcDataReader reader = CMD.ExecuteReader();
reader.Read();
checkBox1.Checked = reader.GetBoolean(0);                

no problem wiil occur. 不会出现任何问题。

BUT!! 但!!

I need to ascertain the column is Null or not,so I use if-statement as below: 我需要确定列是否为Null,所以我使用if语句,如下所示:

OdbcDataReader reader = CMD.ExecuteReader();
reader.Read();
if (reader.IsDBNull(0) == false)
    checkBox1.Checked = reader.GetBoolean(0);

It will occur InvalidCastException error on reader.GetBoolean(0) 它将在reader.GetBoolean(0)上发生InvalidCastException错误。

I have no idea about this problem,does somebody can help me? 我不知道这个问题,有人可以帮我吗? please~ 拜托〜


According to antiduh's valuable comment, I found that if reader.GetValue(0) isn't in if-statement as below: 根据antiduh的宝贵评论,我发现if reader.GetValue(0)不在if语句中,如下所示:

OdbcDataReader reader = CMD.ExecuteReader();
reader.Read();
var foo = reader.GetValue(0);

It wiil return True or False. 它将返回True或False。

But if reader.GetValue(0) is in interior of if-statement as below: 但是,如果reader.GetValue(0)位于if语句的内部,如下所示:

OdbcDataReader reader = CMD.ExecuteReader();
reader.Read();
if (reader.IsDBNull(0) == false)
{
    var foo = reader.GetValue(0);
}

It will return 1 or 0. 它将返回1或0。

So if I use GetBoolean to transform 1 or 0 to bool data type,will occur InvalidCastException. 因此,如果我使用GetBoolean将1或0转换为bool数据类型,则会发生InvalidCastException。

Now I know what happen on my code,but still don't know what the difference between two codes. 现在我知道我的代码会发生什么,但仍然不知道两个代码之间的区别。


Thanks antiduh's valuable comment again, 再次感谢antiduh的宝贵评论,

I try this code that antiduh provide below: 我尝试以下antiduh提供的代码:

OdbcDataReader reader = CMD.ExecuteReader();
reader.Read();
if (reader.IsDBNull(0) == false)
{
    try
    {
        checkBox1.Checked = reader.GetBoolean(0);
    }
    catch ( InvalidCastException e ) {
        object doubleCheck = reader.GetValue(0);
        Console.WriteLine( "Tried to cast this type: " + doubleCheck.GetType() );
    }
}

It wiil return: Tried to cast this type:System.String 它将返回:尝试强制转换此类型:System.String

And doubleCheck.ToString() is 1 or 0. 并且doubleCheck.ToString()是1或0。

Its likely that the value in that row and column is not actually a bool. 该行和列中的值实际上可能不是布尔值。 Add some temporary debugging code to catch the exception and call GetValue instead, and look to see what you're actually getting back. 添加一些临时调试代码以捕获异常,然后调用GetValue,然后看一下您实际返回的内容。

OdbcDataReader reader = CMD.ExecuteReader();
reader.Read();
if (reader.IsDBNull(0) == false)
{
    try {
        checkBox1.Checked = reader.GetBoolean(0);
    }
    catch ( InvalidCastException e ) {
        object doubleCheck = reader.GetValue(0);

        Console.WriteLine( "Tried to cast this type: " + doubleCheck.GetType() );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM