C# 为什么用此代码获取“InvalidOperationException:无当前行”?
我正在尝试使用下面的GetInventoryRecordForUPC方法从表中检索一行。我可以通过输入SELECT invName、line_id、ref_no、upc_代码、描述、部门、供应商_id、upc_包装尺寸、包装尺寸、id、单位成本、单位清单、单位数量、库存中的新项目来检索LINQPad中的行fine,其中upc_代码='76145513',在我的测试场景中,upc中传递的值是76145513 但当以下方法运行时,应用程序崩溃,日志文件包含:C# 为什么用此代码获取“InvalidOperationException:无当前行”?,c#,sqlite,windows-ce,invalidoperationexception,C#,Sqlite,Windows Ce,Invalidoperationexception,我正在尝试使用下面的GetInventoryRecordForUPC方法从表中检索一行。我可以通过输入SELECT invName、line_id、ref_no、upc_代码、描述、部门、供应商_id、upc_包装尺寸、包装尺寸、id、单位成本、单位清单、单位数量、库存中的新项目来检索LINQPad中的行fine,其中upc_代码='76145513',在我的测试场景中,upc中传递的值是76145513 但当以下方法运行时,应用程序崩溃,日志文件包含: System.InvalidOperat
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;
}
}
库存类的声明方式如下:
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
}
为什么在约翰·斯坦贝克的宠物卷毛狗身上会出现一个例外:没有当前行 尝试调用rdr。读取:
您是否将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也出现了同样的异常。我使用以下命令恢复到1.0.94.0
Install-Package System.Data.SQLite.Core -Version 1.0.94.0
你不需要调用rdr.Read吗?应该只有一条记录,但我会添加中断,以防没有双关语。break是以舞蹈命名的唯一关键词吗?如果是零或一,那么就用If替换while,不需要break,我不知道;我现在从事不同的工作,做完全不同的事情,我想不起这件事的细节。。。
Install-Package System.Data.SQLite.Core -Version 1.0.94.0