[英]Why does SQLite tell me "No current row"?
The query being sent is good;发送的查询是好的; running it:
运行它:
SELECT line_id, description, department, upc_pack_size, pack_size, unit_cost, unit_list FROM Inventory WHERE siteNum = "03" AND upc_code = "76145513"
...in LINQPad returns a record with these values: ...在 LINQPad 中返回具有以下值的记录:
line_id = 0
description = [empty string]
department = 2.99
upc_pack_size = 333
pack_size = 333
unit_cost = 50.01
unit_list = 50.99
But the code:但是代码:
public List<String> GetDynamicINVValsForUPCCode(String qry, String siteNum, String upcCode)
{
ExceptionLoggingService.Instance.WriteLog(String.Format("Reached TestHHSDBUtils.GetDynamicINVValsForUPCCode(); siteNum is {0}; upcCode is {1}; qry is {2}", siteNum, upcCode, qry));
List<String> dsdValsForUPCCode = new List<string>();
try
{
using (SQLiteConnection conn = new SQLiteConnection(HHSUtils.GetDBConnection()))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(qry, conn))
{
cmd.Parameters.Add(new SQLiteParameter("upcCode", upcCode));
cmd.Parameters.Add(new SQLiteParameter("SiteNum", siteNum));
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
dsdValsForUPCCode.Add(Convert.ToString(rdr["Line_id"])); // Line_id, int32
dsdValsForUPCCode.Add(Convert.ToString(rdr["Description"])); // Description
dsdValsForUPCCode.Add(Convert.ToString(rdr["Department"])); // Department
dsdValsForUPCCode.Add(Convert.ToString(rdr["Upc_pack_size"])); // Upc_pack_size (int32)
dsdValsForUPCCode.Add(Convert.ToString(rdr["Pack_size"])); // Pack_size (int32)
dsdValsForUPCCode.Add(Convert.ToString(rdr["Unit_qty"])); // Unit_qty (single)
}
}
return dsdValsForUPCCode;
}
}
catch (Exception ex)
{
String msgInnerExAndStackTrace = String.Format("{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
ExceptionLoggingService.Instance.WriteLog(String.Format("From TestHHSDBUtils.GetDynamicINVValsForUPCCode: {0}", msgInnerExAndStackTrace));
return null;
}
}
...fails, the err being logged as: ...失败,错误记录为:
Message: From TestHHSDBUtils.GetDynamicINVValsForUPCCode: No current row; Inner Ex: ; Stack Trace: at System.Data.SQLite.SQLiteDataReader.CheckValidRow()
at System.Data.SQLite.SQLiteDataReader.GetValue(Int32 i)
at System.Data.SQLite.SQLiteDataReader.get_Item(String name)
at HHS.TestHHSDBUtils.GetDynamicINVValsForUPCCode(String qry, String siteNum, String upcCode)
Why does it say "no current row" when there is one?为什么在有一行时显示“无当前行”?
While I'm not explicitly familiar with the SqlLiteReader, but the rest of the readers require you to read the first row.虽然我不是很熟悉 SqlLiteReader,但其余的读者要求您阅读第一行。 I'm using an
if
statement but where you are expecting numerrous rows you would use a while
.我正在使用
if
语句,但在您期望大量行的地方,您将使用while
。 The DataReader.Read() returns a boolean to indicate if a record has been read. DataReader.Read() 返回一个布尔值以指示是否已读取记录。 https://msdn.microsoft.com/en-us/library/haa3afyz%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/haa3afyz%28v=vs.110%29.aspx
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
if (rdr.Read()){
dsdValsForUPCCode.Add(Convert.ToString(rdr["Line_id"])); // Line_id, int32
dsdValsForUPCCode.Add(Convert.ToString(rdr["Description"])); // Description
dsdValsForUPCCode.Add(Convert.ToString(rdr["Department"])); // Department
dsdValsForUPCCode.Add(Convert.ToString(rdr["Upc_pack_size"])); // Upc_pack_size (int32)
dsdValsForUPCCode.Add(Convert.ToString(rdr["Pack_size"])); // Pack_size (int32)
dsdValsForUPCCode.Add(Convert.ToString(rdr["Unit_qty"])); // Unit_qty (single)
}
}
When you set the parameters, the parameter name needs the @
prefix to work.设置参数时,参数名称需要
@
前缀才能起作用。 So your code should instead read:所以你的代码应该改为:
cmd.Parameters.Add(new SQLiteParameter("@upcCode", upcCode));
cmd.Parameters.Add(new SQLiteParameter("@SiteNum", siteNum));
Additionally (as pointed out in other answers) you also need to perform a Read()
on the reader first:此外(如其他答案中所指出的)您还需要先在阅读器上执行
Read()
:
rdr.Read();
I belive SQLite requires parameters to have '@', ':' or '$' prefixed names.我相信 SQLite 要求参数具有 '@'、':' 或 '$' 前缀名称。 Source .
来源。
So your query should be like:所以你的查询应该是这样的:
SELECT line_id, description, department, upc_pack_size, pack_size, unit_cost, unit_list FROM Inventory WHERE siteNum = @siteNum AND upc_code = @upcCode
And your paramaters like this:你的参数是这样的:
cmd.Parameters.Add(new SQLiteParameter("@siteNum", siteNum));
cmd.Parameters.Add(new SQLiteParameter("@upcCode", upcCode));
这里同样的问题,而使用SQLite构建vb.net,并且发现了使用后executereader
,你需要使用read()
从对象sqlitedatareader
阅读
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.