Reader sqlite 查询后丢失值

Reader losing values after sqlite query

所以我试图从现有的 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;
}

方法 HasRows() returns true 如果 reader 包含一行或多行或 false 如果不包含,但它不会前进 reader到下一行。

您必须使用方法 Read():

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