简体   繁体   English

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

[英]How to retrieve byte array from SQLite database in UWP using C#

I've searched for too long but couldn't find any answer. 我已经搜索了很长时间,但是找不到任何答案。

I'm working on a UWP app which stores BitmapImage data (converted to byte array) into a blob field in an SQLite3 database. 我正在开发一个UWP应用,该应用将BitmapImage数据(转换为字节数组)存储到SQLite3数据库的blob字段中。 However, I can't find a way to retrieve the data once it's saved in the database. 但是,一旦将数据保存在数据库中,我将找不到一种检索数据的方法。

All the solutions I've seen use the GetBytes method from Microsoft.Data.SQLite.SQLiteDataReader but Microsoft's documentation indicates method is "not supported". 我见过的所有解决方案都使用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 https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.data.sqlite.sqlitedatareader.getbytes?view=msdata-sqlite-1.1.0

Can anyone please show some sample code how to retrieve the blob from the SQLite 3 database and convert it to a BitmapImage for display? 任何人都可以显示一些示例代码,说明如何从SQLite 3数据库中检索Blob并将其转换为BitmapImage进行显示吗?

Thanks in advance for your help. 在此先感谢您的帮助。

That is a really minimal version of the work required but that gives you the step that will store picture data as btye[] 's also that will retrieve the data as a byte array. 这实际上是所需工作的最低限度版本,但它为您提供了将图片数据存储为btye[]的步骤,并且还将以字节数组的形式检索数据。

I won't go into full depth of the entity framework because that isn't a part of the question, I believe this link does a good job. 我不会深入介绍实体框架,因为这不是问题的一部分,我相信此链接可以很好地完成工作。 Microsoft Documentation . Microsoft文档

Disclaimer I have never used the entity framework with UWP, as well I have never used a SQL Lite database, there may be things with the amount of data and all the available types that can be stored in the database. 免责声明我从未将实体框架与UWP一起使用,也从未使用过SQL Lite数据库,因此可能存在一些数据量以及可以存储在数据库中的所有可用类型。

As for the converting byte[] to BitMap in UWP, look here . 至于在UWP中将byte[]转换为BitMap,请看这里

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();
        }
     });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM