OdbcDataReader.GetBoolean() 抛出强制转换对于 bool 列无效异常的奇怪问题

发布于 2024-07-29 04:19:19 字数 368 浏览 4 评论 0原文

好吧,这让我彻底发疯了。 我在 PostgreSQL 数据库中有一个表,我试图使用 OdbcDataReader.GetBoolean(int col) 获取特定记录的布尔列值。

问题是 GetBoolean() 不断抛出 cast is not valid 异常,即使几天前相同的代码还有效。 更奇怪的是相同的代码在另一种形式下工作得很好。

与我的应用程序的先前工作版本相比,唯一的更改是向表中添加一列。 也就是说,我需要的列的索引没有改变。 哦,是的,使用 GetValue() 获取值,然后对结果调用 GetType() 将返回 System.String,并且 true/false 值将转换为 1/0。

我完全没有主意了。

Ok, this one is driving me completely crazy. I have a table in a PostgreSQL database and I'm trying to get the value a boolean column for a specific record with OdbcDataReader.GetBoolean(int col).

The problem is that GetBoolean() keeps throwing a cast is not valid exception even though the same code worked just several days ago. What's even weirder is the same code works fine in another form.

The only change made from the previous working version of my application was adding a column to the table. That said, the index of the column that I need hasn't changed. Oh yeah, getting the value with GetValue() and then calling GetType() on the result returns System.String and the true/false values get translated to 1/0.

I'm all out of ideas.

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

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

发布评论

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

评论(3

离线来电— 2024-08-05 04:19:19

虽然我仍然不知道是什么导致了这个异常,但我已经设法想出了有效的代码(不是我的应用程序中的实际代码):

val1 = reader.GetInt32(0);
val2 = reader.GetBoolean(4);
val3 = reader.GetBoolean(8);

但是,这不是:

val3 = reader.GetBoolean(8);
val1 = reader.GetInt32(0); // causes invalid cast exception
val2 = reader.GetBoolean(4);

显然,列顺序与它有关。

While I still have no idea what is causing this exception, I've managed to come up with code that works (not actual code from my app):

val1 = reader.GetInt32(0);
val2 = reader.GetBoolean(4);
val3 = reader.GetBoolean(8);

This, however, does not:

val3 = reader.GetBoolean(8);
val1 = reader.GetInt32(0); // causes invalid cast exception
val2 = reader.GetBoolean(4);

Apparently, the column order has something to do with it.

橘和柠 2024-08-05 04:19:19

我不认为将 System.String 传递给 GetBoolean() 会对你有用 - 正如你从异常中看到的那样,你得到了一个 InvalidCastException ,这意味着在内部某个地方它试图做这样的事情:

string s = "true";
bool b = (bool)s;

显然行不通的。

如果您可以看到 ODBC 正在向您传回 System.String,那么您需要使用 Convert.ToBoolean()、bool.TryParse() 或 bool.Parse,具体取决于什么最适合您的代码的其余部分。

至于为什么它以前有效而现在不起作用 - 是否有人将数据库字段上的基础数据类型更改为基于字符的类型?

我们用这种模式从 OdbcDataReader 中获取布尔数据:

data.Gender = reader["Gender"] == DBNull.Value ? 
    false : Convert.ToBoolean(reader["Gender"]);

是的,很费力。 但它对我们来说效果很好。 此代码的基础数据库是 Access(“是/否”字段类型)或 MS SQL Server(“位”字段类型)。

I don't think passing a System.String to GetBoolean() is going to work for you - as you can see from your exception you're getting an InvalidCastException, which means that internally somewhere it's trying to do something like this:

string s = "true";
bool b = (bool)s;

That clearly won't work.

If you can see that ODBC is passing you back a System.String then you want to use either Convert.ToBoolean(), bool.TryParse(), or bool.Parse, depending on what exactly fits best with the rest of your code.

As to why it WAS working and now isn't - has someone else changed the underlying data type on the database field to a character-based type?

This pattern is what we use to get boolean data out of an OdbcDataReader:

data.Gender = reader["Gender"] == DBNull.Value ? 
    false : Convert.ToBoolean(reader["Gender"]);

Laborious, yes. But it works well for us. The underlying database for this code is Access ("yes/no" field type) or MS SQL Server ("bit" field type).

南冥有猫 2024-08-05 04:19:19

我遇到了同样的问题,这实际上是由于我在调用的 SP 中进行了无效的转换...

在存储过程中

declare @colA INT -- should have been a float!!!
set @colA = select someValue from someTable

select @someVar, someColumn
from someOtherTable

 //the line below works, even though it should have the SQL datatype is FLOAT
 _someVar = reader[2] != DBNull.Value ? reader.GetDouble[2] : 0;
  //the following line blows up w/invalid cast exception, even though it is correct
 _someColumn = reader[3] != DBNull.Value ? reader.GetBoolean[3] : false;

I ran into the same issue, it was actually due to an invalid cast I had in the SP I was calling...

IN the Stored Proc

declare @colA INT -- should have been a float!!!
set @colA = select someValue from someTable

select @someVar, someColumn
from someOtherTable

 //the line below works, even though it should have the SQL datatype is FLOAT
 _someVar = reader[2] != DBNull.Value ? reader.GetDouble[2] : 0;
  //the following line blows up w/invalid cast exception, even though it is correct
 _someColumn = reader[3] != DBNull.Value ? reader.GetBoolean[3] : false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文