【问题标题】:Reader losing values after sqlite query阅读器在 sqlite 查询后丢失值
【发布时间】:2021-08-27 13:29:23
【问题描述】:

所以我试图从现有的 SQLite 数据库中读取值,它似乎最初可以工作,但是在查询之后,当我尝试读取它们时,它似乎丢失了值。

我在这里做错了什么?

https://www.youtube.com/watch?v=Yvj6BTjts3M

public ObservableCollection<Asset> GetAllAssets()
{
    _DbConn.Open();

    var command = _DbConn.CreateCommand();
    var allAssets = new ObservableCollection<Asset>();

    command.CommandText =
    @"
        SELECT * 
        FROM asset 
    ";

    using (var reader = command.ExecuteReader())
    {
        while (reader.HasRows)
        {
            Asset asset = new Asset();
            asset.AssetId = reader.GetInt32(0);
            asset.AssetTypeId = reader.GetInt32(1);
            asset.Name = reader.GetString(2);
            asset.FileLocation = reader.GetString(3);
            asset.IsCustom = reader.GetBoolean(4);
            asset.AssetSectionId = reader.GetInt32(5);
            asset.ThumbFileLocation = reader.GetString(6);

            allAssets.Add(asset);
        }
    }

    _DbConn.Close();

    return allAssets;
}

【问题讨论】:

  • 您应该在视频下方写下评论,说明教的东西不起作用。并发布此问题的链接;)

标签: c# sqlite uwp


【解决方案1】:

如果reader 包含一行或多行,则HasRows() 方法返回true,否则返回false,但它不会将reader 推进到下一行。

你必须使用Read()的方法:

while (reader.Read())
......................

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2011-09-12
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多