简体   繁体   English

为什么 SQLite 告诉我“没有当前行”?

[英]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.

相关问题 为什么 Resharper 告诉我可能存在 NullReferenceException? - Why does Resharper tell me there is a possible NullReferenceException? 为什么Visual Studio告诉我数据库不存在而存在? - Why does Visual Studio tell me that a database exists when it does not? 为什么ReSharper告诉我“隐式捕获关闭”? - Why does ReSharper tell me “implicitly captured closure”? 为什么ReSharper告诉我这个表达式总是正确的? - Why does ReSharper tell me this expression is always true? 为什么 Visual Studio 告诉我 AddJsonFile() 方法没有定义? - Why does Visual Studio tell me that the AddJsonFile() method is not defined? 为什么此代码告诉我找不到类型或名称空间? - Why does this code tell me the type or namespace could not be found? 任何人都可以告诉我为什么AllowDrop不能与文本框一起使用 - Can anyone tell me why the AllowDrop does not work with text boxes 为什么在 Azure Functions 中使用 SQLite 会给我一个 DLLNotFoundException? - Why does using SQLite in Azure Functions give me a DLLNotFoundException? 序列化到MemoryStream会导致OutOfmemoryException,但序列化到FileStream却不会。 谁能告诉我为什么? - Serializing to MemoryStream causes an OutOfmemoryException, but serializing to a FileStream does not. Can anyone tell me why? 为什么我的函数告诉我检测到无法访问的代码? C# - Why does my function tell me there's an unreachable code detected? c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM