如何使用C#从UWP中的SQLite数据库中检索字节数组

时间:2017-08-03 11:56:02

标签: c# xaml uwp

我搜索的时间太长但找不到任何答案。

我正在开发一个UWP应用程序,它将BitmapImage数据(转换为字节数组)存储到SQLite3数据库的blob字段中。但是,一旦将数据保存在数据库中,我找不到检索数据的方法。

我见过的所有解决方案都使用Microsoft.Data.SQLite.SQLiteDataReader中的GetBytes方法,但Microsoft的文档表明方法“不受支持”。 https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlite.sqlitedatareader.getbytes?view=msdata-sqlite-1.1.0

有人可以展示一些示例代码,如何从SQLite 3数据库中检索blob并将其转换为BitmapImage进行显示?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是所需工作的真正最小版本,但它为您提供了将图片数据存储为btye[]的步骤,它还将数据作为字节数组进行检索。

我不会深入了解实体框架,因为这不是问题的一部分,我相信这个链接做得很好。 Microsoft Documentation

免责声明我从未将实体框架与UWP一起使用过,我从未使用过SQL Lite数据库,可能存在大量数据和可存储的所有可用类型的东西在数据库中。

至于在UWP中将byte[]转换为BitMap,请查看here

public class ProfilePictures
{
    public int ProfilePicturePK {get;set;}
    public byte[] PictureData {get;set;}
}

... all the other entity setup, see link for entity setup

public byte[] GetPictureData(int id)
{
    return GetPictureDataAsync(id).Wait();
}

//If you want to make things a little more UI and thread friendly
public async Task<byte[]> GetPictureDataAsync(int id)
{
     await Task.Factory.StartNew(()=>{
        using(var db = Context)
        {
            return (from pictures in db.ProfilePictures where pictures.ProfilePicturePK == id select pictures).SingleOrDefault();
        }
     });
}
相关问题