为什么我使用此代码得到 "InvalidOperationException: No current row"?

Why do I get "InvalidOperationException: No current row" with this code?

我正在尝试使用下面的 GetInventoryRecordForUPC() 方法从 table 中检索一行。我可以通过输入“SELECT invName, line_id, ref_no, upc_code, description, department, vendor_id 来检索 LINQPad 中的行, upc_pack_size, pack_size, id, unit_cost, unit_list, unit_qty, new_item FROM Inventory WHERE upc_code = '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)

该方法已注释掉 table 的创建语句以显示列的名称和数据类型。

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

库存 class 是这样声明的:

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:没有当前行”?!?

尝试调用 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;

您是否将 SQLIte 作为 NuGet 包下载?

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

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

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

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