为什么我得到" InvalidOperationException:没有当前行"用这个代码?

时间:2015-01-30 23:04:16

标签: c# sqlite windows-ce invalidoperationexception

我尝试使用下面的 GetInventoryRecordForUPC()方法从表中检索行。我可以通过输入" SELECT invName,line_id,ref_no,upc_code,description,department,vendor_id,upc_pack_size,pack_size,id,unit_cost,unit_list,unit_qty,new_item FROM Inventory WHERE upc_code =来检索LINQPad中的行。 ' 76145513' " (在我的测试场景中传入的值" upc"" 76145513")。

但是当下面的方法运行时,应用程序崩溃,日志文件包含:

System.InvalidOperationException: No current row
   at System.Data.SQLite.SQLiteDataReader.CheckValidRow()
   at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
   at System.Data.SQLite.SQLiteDataReader.GetString(Int32 i)
   at HHS.TestHHSDBUtils.GetInventoryRecordForUPC(String upc)

该方法将表的create语句注释掉,以显示列的名称和数据类型。

public Inventory GetInventoryRecordForUPC(String upc)
{
    ExceptionLoggingService.Instance.WriteLog("Reached 
TestHHSDBUtils.GetInventoryRecordForUPC");
    Inventory inv = new Inventory();
    using (SQLiteConnection conn = new 
SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        const string qry = "SELECT invName, line_id, ref_no, upc_code, 
description, department, vendor_id, upc_pack_size, pack_size, id, unit_cost, 
unit_list, unit_qty, new_item FROM Inventory WHERE upc_code = @UPCCode";
        //CREATE TABLE Inventory(invName TEXT, line_id INTEGER, ref_no 
TEXT, upc_code TEXT, description TEXT, department REAL, vendor_id TEXT, 
upc_pack_size INTEGER, pack_size INTEGER, id TEXT, unit_cost REAL, unit_list 
REAL, unit_qty REAL, new_item INTEGER, siteNum TEXT, Created TEXT, Modified 
TEXT)";

        using (SQLiteCommand cmd = new SQLiteCommand(qry, conn))
        {
            cmd.Parameters.Add(new SQLiteParameter("UPCCode", upc));
            using (SQLiteDataReader rdr = cmd.ExecuteReader())
            {
                inv.invName = rdr.GetString(0);
                inv.line_id = rdr.GetInt32(1);
                inv.ref_no = rdr.GetString(2);
                inv.upc_code = rdr.GetString(3);
                inv.description = rdr.GetString(4);
                inv.department = rdr.GetFloat(5);
                inv.vendor_id = rdr.GetString(6);
                inv.upc_pack_size = rdr.GetInt32(7);
                inv.pack_size = rdr.GetInt32(8);
                inv.id = rdr.GetString(9);
                inv.unit_cost = rdr.GetFloat(10);
                inv.unit_list = rdr.GetFloat(11);
                inv.unit_qty = rdr.GetFloat(12);
                inv.new_item = rdr.GetInt32(13);
            }
        }
        return inv;
    }
}

Inventory类以这种方式声明:

public class Inventory
{
    public String invName { get; set; }
    public int line_id { get; set; }
    public String ref_no { get; set; }
    public String upc_code { get; set; }
    public String description { get; set; }
    public double department { get; set; }
    public String vendor_id { get; set; }
    public int upc_pack_size { get; set; }
    public int pack_size { get; set; }
    public String id { get; set; } // id, here?
    public double unit_cost { get; set; } // REAL in SQLite
    public double unit_list { get; set; } // REAL in SQLite
    public double unit_qty { get; set; }  // REAL in SQLite
    public int new_item { get; set; }
    // Create Table code adds TEXT siteNum, Created and Modified columns
}

为什么John Steinbeck的宠物贵宾犬会导致" InvalidOperationException:没有当前行"?!?

3 个答案:

答案 0 :(得分:3)

尝试拨打rdr.Read()

using (SQLiteCommand cmd = new SQLiteCommand(qry, conn))
{
    cmd.Parameters.Add(new SQLiteParameter("UPCCode", upc));
    using (SQLiteDataReader rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            // Set inv properties
            break; // (if you only want the first item returned)
        }
    }
}

return inv;

答案 1 :(得分:3)

升级到版本1.0.96.0后,我也使用System.Data.SQLite.Core获得了相同的异常。我使用以下nuget命令恢复到1.0.94.0

Install-Package System.Data.SQLite.Core -Version 1.0.94.0

答案 2 :(得分:2)

您是否将SQLIte下载为NuGet包?

我不确定实际原因是什么,但我得到了与使用System.Data.SQLite.Core版本1.0.96.0时相同的异常。

然后我降级到版本1.0.94.0,现在一切正常。 (我的本地磁盘上有1.0.94.0 dll)