在C#中使用SQLiteDataReader.Read()方法在while循环中未读取检索到的数据

osh3o9ms  于 2023-06-06  发布在  SQLite
关注(0)|答案(1)|浏览(328)

SQLiteDataReader.Read()不会回复false,即使它包含数据。
1.我一直在一些数据控制器上运行测试,但每次我从db中检索数据时,它都因为某种原因无法工作,确切地说,Read()没有进入循环来读取其中的数据。
1.连接成功,数据已读取,我在调试器中观察到了,请看下面的截图。
下面的代码是我正在做的,预计将进入try块中的while循环。

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
    {
        SQLiteCommand command = new SQLiteCommand(connection);
        SQLiteDataReader reader = null;
        //Make sure to ignore class names below, the problem isn't related to them they are changed for sake of demonstration
        Dictionary<string, SomeDTO> output = new Dictionary<string, SomeDTO>();

        try
        {
            command.CommandText = $"SELECT * FROM {TableName}";
            
            connection.Open();
            reader = command.ExecuteReader();

            while (reader.Read())
            { //This loop is never entered
                output.Add(reader.GetString(0), ConvertReaderToObject(reader));
            }
        }
        catch (Exception ex)
        {
            Log.Error("SOME-LOG IRRELEVANT"+ex.Message);
        }
        finally
        {
            if(reader != null)
            {
                reader.Close();
            }
            command.Dispose();
            connection.Close();
        }

        return output;
    }
}

在下面的图片中,您可以看到读取器对象上的调试器详细信息,数据已按预期读取...

我看了这些问题,但找不到答案:
123

ckx4rj1h

ckx4rj1h1#

@TimSchmelter回答,发帖解决这个问题不会引起人们的注意。调试器本身使用reader中的数据,因此我可以看到调试器中的数据。直接从catch开始调试,将显示while循环中有一个异常。

相关问题