【发布时间】:2016-07-25 05:16:25
【问题描述】:
今天我正在处理如何将字节数组保存到 SQLite 数据库的问题。当我将 base[] 保存到数据库时,看起来该属性保存了该数组,但实际上当我在设备上重新启动应用程序时它并没有保存到数据库。也许我必须先将 byte[] 转换为 base 64,然后才能将其放入数据库?如果是,我该怎么做?也可能有另一种方法保存到 sqlite 数据库,而不转换为 base64?
这是我的对象类:
public class Unit
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public bool IsStarted { get; set; }
public byte[] CImage { get; set; }
}
这里我从图库中选择图片并设置为可绑定值:
IGalleryImageService galleryService = Xamarin.Forms.DependencyService.Get<IGalleryImageService>();
galleryService.ImageSelected += (o, imageSourceEventArgs) =>
{
ActiveP.CImageBindable = imageSourceEventArgs.ImageSource;
MemoryStream st = new MemoryStream(imageSourceEventArgs.ImageSource);
ImageSource imgSource = ImageSource.FromStream(() => st);
(ActiveP.Page as PTemplate).CImage.Source = imgSource;
};
galleryService.SelectImage();
这是我正在更新数据库或将对象插入数据库的另一个 Unit 类:
private void UpdateObjectToDB()
{
PUnit pUinit = new PUnit()
{
Id = this.Id,
IsStarted = this.IsStarted,
CImage = this.CImage
};
App.database.Update(pUinit);
}
public int InsertObjectToDB()
{
PUnit pUnit = new PUnit()
{
IsStarted = this.IsStarted,
CCImage = this.CImage
};
return App.database.AddItem(pUnit);
}
我必须在哪里转换以及如何实现?感谢您的回答或建议。
【问题讨论】:
-
这是一个教程,其中包括如何将
byte[]保存到 SQLite 数据库:stackoverflow.com/questions/41337487/…
标签: c# sqlite xamarin bytearray xamarin.forms