错误:“行/列不存在数据”使用 OdbcDataReader

发布于 2024-12-22 12:14:49 字数 842 浏览 1 评论 0原文

尽管我知道我正在执行的确切 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();
        }

任何人都可以告诉我为什么会发生这种情况的原因吗?该查询应该返回一个结果,我目前这样做只是为了确保它正在工作,但事实似乎并非如此。任何帮助将不胜感激。

Even though I know that there is data for the exact SQL query I'm performing, for the fact that I am doing the SQL query directly on the database, I continually get an exception saying that no data exists. My code is below:

      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();
        }

Can anyone give me reasons as to why this would be happening. The query should return a single result, and I'm currently only doing this to ensure that it is working, which it doesn't appear to be. Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

余罪 2024-12-29 12:14:49

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

dbReader.Read()

或者当然如果有多行:

while (dbReader.Read())

After ExecuteReader is called, the reader is positioned before the first returned record. To read the first record you need to call Read()

dbReader.Read()

Or of course if there are multiple rows:

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