0

当我输入此代码时:

        query = "SELECT DateString FROM table WHERE DateArrivage = 'DefaultValue' ";
        OdbcDataReader DbR = ObAccess.SQLSELECT(query);
        int fCount = DbR.FieldCount;
        if (fCount > 0)
        {

            DateWithoutDate = DbR.GetString(0); // i wan't only first value
            DbR.Close();
        }
        else
        {
            int row_count = DbR.RecordsAffected;
            Console.WriteLine("Query affected " + row_count + " row(s)");
        }

它给出了我在标题中写的错误,有人可以帮助我吗?

4

1 回答 1

0

您没有调用Read()数据阅读器,因此从逻辑上讲,它位于第一行之前。我怀疑你实际上想要:

query = "SELECT DateString FROM table WHERE DateArrivage = 'DefaultValue' ";
using (OdbcDataReader reader = ObAccess.SQLSELECT(query))
{
    if (reader.Read())
    {
        DateWithoutDate = reader.GetString(0);
    }
    else
    {
        // Whatever you want to do if there are no rows
    }
}

同样令人担忧的是,您有类似ObAccessand SQLSELECT... 之类的名称,而且您似乎没有在此代码中打开和关闭连接,这通常是个好主意。(不要尝试自己打开单个连接并重用它 - 让底层数据库连接池为您做这件事。)

于 2013-05-11T17:31:52.527 回答