C# 如何使用sqlite数据库上载和检索图像
我的问题是在Xamarin中。使用SQLite数据库的表单可以上载图像并将其检索到同一位置(如个人资料图片)C# 如何使用sqlite数据库上载和检索图像,c#,sqlite,xamarin.forms,C#,Sqlite,Xamarin.forms,我的问题是在Xamarin中。使用SQLite数据库的表单可以上载图像并将其检索到同一位置(如个人资料图片) 我在google上搜索过,但是我找不到类似的东西,这在xamarin.Forms中是可以做到的,使用SQLite本地数据库是的,这是可能的。看看这个小例子: 只需将图像转换为字节数组,即可使用: byte[]myimage=System.IO.File.ReadAllBytes(“pathtoimage”) 将映像插入sql lite数据库: public bool InsertMess
我在google上搜索过,但是我找不到类似的东西,这在xamarin.Forms中是可以做到的,使用SQLite本地数据库是的,这是可能的。看看这个小例子: 只需将图像转换为字节数组,即可使用:
byte[]myimage=System.IO.File.ReadAllBytes(“pathtoimage”)
将映像插入sql lite数据库:
public bool InsertMessage()
{
SqliteCommand cmd = new SqliteCommand(con);
cmd.CommandText = "INSERT INTO Images(Data) VALUES (@img)";
cmd.Prepare();
cmd.Parameters.Add("@img", DbType.Binary, myimage.Length);
cmd.Parameters["@img"].Value = myimage;
cmd.ExecuteNonQuery();
con.Close();
}
要检索图像并将其存储到文件,请执行以下操作:
using(SqliteConnection con = new SqliteConnection(cs))
{
con.Open();
SqliteCommand cmd = new SqliteCommand(con);
cmd.CommandText = "SELECT Data FROM Images WHERE Id=1";
byte[] data = (byte[]) cmd.ExecuteScalar();
try
{
if (data != null)
{
File.WriteAllBytes("woman2.jpg", data);
myimage = data
} else
{
Console.WriteLine("Binary data not read");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
con.Close();
}
如果要将图像保存到位图变量中:
ByteArrayInputStream inputStream = new ByteArrayInputStream(myimage);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
是的,可以使用SQLite数据库!要阅读的任何文档我在xamarin表单中使用的SQLite都是xamarin表单将支持它这是如何插入图像我如何检索它。我添加了检索方法MyImageLogo=MyByteArray变量What this variable and is MyByteArray是一种数据类型它不支持xamarinyou miss cmd.ExecuteOnQuery();尝试删除cmd.prepare()并添加cmd.ExecuteOnQuery();