为什么用此代码得到“ InvalidOperationException:无当前行”?

B. *_*non 0 c# sqlite invalidoperationexception windows-ce

我正在尝试使用下面的GetInventoryRecordForUPC()方法从表中检索行。我可以通过输入检索LINQPad排精“ SELECT invName,line_id,ref_no,upc_code,说明,部门,VENDOR_ID,upc_pack_size,pack_size,ID,UNIT_COST,unit_list,unit_qty,NEW_ITEM从库存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)
Run Code Online (Sandbox Code Playgroud)

该方法将表的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

清单类是这样声明的:

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
}
Run Code Online (Sandbox Code Playgroud)

为什么在John Steinbeck的宠物贵宾犬中会导致“ InvalidOperationException:无当前行 ”?!

Ruf*_*s L 5

尝试致电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;
Run Code Online (Sandbox Code Playgroud)