【问题标题】:Oracle DataReader returns 1 row but Read method returns falseOracle DataReader 返回 1 行,但 Read 方法返回 false
【发布时间】:2011-01-13 01:56:21
【问题描述】:

我有一个基本的 sql 语句来查找用户并返回一条记录,但是当我运行一段代码时说 if(myReader.Read()) 它返回 false。我单步执行了代码并检查了 reader 对象,它实际上包含一条记录。下面是代码。

sql: 选择用户名、用户密码、用户状态 来自用户 其中 users.user_id = 123

    System.Data.Common.DbCommand _cmd = this.GetCommand(conn, _dbf, sqlText, CommandType.Text);
     System.Data.Common.DbConnection _cn = _cmd.Connection;
     System.Data.Common.DbDataReader myReader = null;

     _cn.Open();
     using(_cn) {
        myReader = _cmd.ExecuteReader();
        if (myReader.Read())  {
                <object gets built here with user data returned from sql>
          }
       }

【问题讨论】:

  • 从 sqlplus 或 sql developer 运行此查询是否返回结果?
  • 是的,在 toad 中运行 sql 并返回结果。验证连接字符串指向正确的数据库。

标签: c# oracle datareader


【解决方案1】:

试试:

if (myReader.HasRows)
  while (myReader.Read())
  .....

【讨论】:

  • 我会这样做,但这不是我要更改的代码库。只是研究为什么其他开发人员的代码不起作用。可能让他们把它改成这个。
猜你喜欢
  • 2023-03-29
  • 2016-10-19
  • 1970-01-01
  • 2018-09-07
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 2020-04-29
相关资源
最近更新 更多