DataReader 出现奇怪的问题?

发布于 2024-08-26 22:43:27 字数 561 浏览 6 评论 0原文

我在 DataReader 方面遇到了一个奇怪的问题。我正在将 OdbcClient 与旧版 RDBMS 系统一起使用。

我正在向数据库发送以下命令。

select Col1, Col2, Col3 from Table1 where Col2 = 'Val1';

然而,在某些情况下,当我通过 DataReader 迭代它时,如下所示。

IDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
  // Get the values from reader

  int col2 = reader.GetValue(1);

}

尽管我在数据库的 Col2 列中具有值,但我得到的 Col2 值为 DBNull。我正确获取了 Col1 和 Col3 的值。这种行为的可能原因是什么?

更新:如果我在调用 cmd.ExecuteQuery() 之前调用 cmd.Prepare(),则此问题已修复。请问有人可以解释这种行为吗?

I am facing a strange issue with DataReader. I am using OdbcClient with a legacy rdbms system.

I am sending follwing command to the database.

select Col1, Col2, Col3 from Table1 where Col2 = 'Val1';

However in certain cases when I iterate it through DataReader as shown below.

IDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
  // Get the values from reader

  int col2 = reader.GetValue(1);

}

I get the Col2 value as DBNull inspite of the fact that I have value in the Col2 column in the database. I am getting the values of Col1 and Col3 correctly. What can be the possible reasons of this behaviour?

Update : This is fixed if I call cmd.Prepare() before calling cmd.ExecuteQuery(). Please can anyone explain this behaviour?

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

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

发布评论

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

评论(2

衣神在巴黎 2024-09-02 22:43:27

Col2的数据类型是什么?您正在使用 GetValue 进行读取,并且数据库中可能有一个无法转换为 Int 的值...是这种情况吗?

What is the data type of Col2? you are reading with GetValue, and maybe the database has a value that is not convertible to Int... is it the case?

ι不睡觉的鱼゛ 2024-09-02 22:43:27

您是否尝试过使用命名参数来阅读:

int col2 = int.parse(reader["Col2"].ToString());

Have you tried to read with named argument:

int col2 = int.parse(reader["Col2"].ToString());

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文