请教SQLite数据库读写BLOB字段
1个回答
推荐于2016-10-15 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517184
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
// 连接字符串 data source=d:\test.db3;password=1234
SQLiteConnectionStringBuilder connStr = new SQLiteConnectionStringBuilder();
connStr.DataSource = @"d:\test.db3";
connStr.Password = "1234";
conn = new SQLiteConnection(connStr.ToString());
conn.Open();
// 插入数据
string SQL = "INSERT INTO test(k) VALUES(:k)";
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
// 取图标,准备插入到数据库BLOB字段
FileStream picStream = File.OpenRead(@"d:\1.bmp");
byte[] bArray = new byte[picStream.Length];
picStream.Read(bArray, 0, bArray.Length);
picStream.Close();
cmd.Parameters.Add("k", DbType.Binary).Value = bArray; // BLOB
cmd.ExecuteNonQuery();
//取出数据
cmd.CommandText = "SELECT k FROM test";
System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MemoryStream streamImage = new MemoryStream(reader["k"] as byte[]);
pictureBox1.Image = Image.FromStream(streamImage);
streamImage.Close(); // 关闭流
}
reader.close();
conn.close();
SQLiteConnectionStringBuilder connStr = new SQLiteConnectionStringBuilder();
connStr.DataSource = @"d:\test.db3";
connStr.Password = "1234";
conn = new SQLiteConnection(connStr.ToString());
conn.Open();
// 插入数据
string SQL = "INSERT INTO test(k) VALUES(:k)";
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
// 取图标,准备插入到数据库BLOB字段
FileStream picStream = File.OpenRead(@"d:\1.bmp");
byte[] bArray = new byte[picStream.Length];
picStream.Read(bArray, 0, bArray.Length);
picStream.Close();
cmd.Parameters.Add("k", DbType.Binary).Value = bArray; // BLOB
cmd.ExecuteNonQuery();
//取出数据
cmd.CommandText = "SELECT k FROM test";
System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MemoryStream streamImage = new MemoryStream(reader["k"] as byte[]);
pictureBox1.Image = Image.FromStream(streamImage);
streamImage.Close(); // 关闭流
}
reader.close();
conn.close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询