求rar , exe , jpg 等各种货色存SQLITE 数据库的完整代码
求rar , exe , jpg 等各种东西存SQLITE 数据库的完整代码!
sqlite 好像是BLOB类型来存这些东西的,网上搜了很多,源码也看了几个,但是没有SQLITE的,在存入和取出都遇到了很多问题,搞不定啊,求完整代码!包括数据库连接!
------解决思路----------------------
如果你用C#,你可以对FileStream做Base64后以文本的形式存入数据库。再解码后写入FileStream的方式还原。
------解决思路----------------------
Base64的方法不可取,转换成文本既费时间又费空间,还是直接存byte[]比较好。
找了一下,以前试验的代码还在:
sqlite 好像是BLOB类型来存这些东西的,网上搜了很多,源码也看了几个,但是没有SQLITE的,在存入和取出都遇到了很多问题,搞不定啊,求完整代码!包括数据库连接!
------解决思路----------------------
如果你用C#,你可以对FileStream做Base64后以文本的形式存入数据库。再解码后写入FileStream的方式还原。
------解决思路----------------------
Base64的方法不可取,转换成文本既费时间又费空间,还是直接存byte[]比较好。
找了一下,以前试验的代码还在:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Data.SQLite;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=t.db3"))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(
"INSERT INTO IMGS (ID , IMAGE) VALUES ( @id_value, @image_value)", conn))
{
using (Image png = Image.FromFile("0.jpg"))
{
using (System.IO.MemoryStream strm = new System.IO.MemoryStream())
{
png.Save(strm, ImageFormat.Jpeg);
cmd.Parameters.Add(new SQLiteParameter("id_value", 1));
cmd.Parameters.Add(new SQLiteParameter("image_value", strm.ToArray()));
cmd.ExecuteNonQuery();
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=t.db3"))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(
"CREATE TABLE IMGS (ID, IMAGE, PRIMARY KEY(ID))", conn))
{
cmd.ExecuteNonQuery();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=t.db3"))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(
"SELECT IMAGE FROM IMGS WHERE ID = 1", conn))
{
byte[] data = (byte[])cmd.ExecuteScalar();
using (System.IO.MemoryStream strm = new System.IO.MemoryStream(data))
{
Image img = Image.FromStream(strm);
pictureBox1.Image = img;
}
}
}
}
}
}