【问题标题】:Error: "No data exists for the row/column" using OdbcDataReader错误:“行/列不存在数据”使用 OdbcDataReader
【发布时间】:2011-12-24 02:20:21
【问题描述】:

即使我知道我正在执行的确切 SQL 查询有数据,但由于我直接在数据库上执行 SQL 查询,我不断收到异常说不存在数据。我的代码如下:

      try
        {
            dbConnection.Open();

            // Process data here.
            OdbcCommand dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = "select forename from tblperson where personcode in (select clientcode from tblclient) and surname = '######'";
            OdbcDataReader dbReader = dbCommand.ExecuteReader();

            Console.WriteLine(dbReader.GetString(0));

            dbReader.Close();
            dbCommand.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            dbConnection.Close();
        }

谁能告诉我为什么会发生这种情况。查询应该返回一个结果,我目前这样做只是为了确保它正在工作,但它似乎没有。任何帮助将不胜感激。

【问题讨论】:

    标签: c# odbc datareader


    【解决方案1】:

    在调用ExecuteReader 之后,读取器位于第一个返回的记录之前。要读取第一条记录,您需要致电Read()

    dbReader.Read()
    

    当然,如果有多行:

    while (dbReader.Read())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2011-09-22
      相关资源
      最近更新 更多