sqlite 好像是BLOB类型来存这些东西的,网上搜了很多,源码也看了几个,但是没有SQLITE的,在存入和取出都遇到了很多问题,搞不定啊,求完整代码!包括数据库连接!
—- 5分
如果你用C#,你可以对FileStream做Base64后以文本的形式存入数据库。再解码后写入FileStream的方式还原。
—-
是存入SQLITE的TEXT类型吗? 给个简单的实例吧!比如把AAA.rar存入和取取要怎么做呢?对FileStream,Base64,完全不懂啊!
—- 15分
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;
}
}
}
}
}
}
—-
谢谢!这个里面的IMAGE存入数据库,sqlite是不是要用BLOB类型存呢?如果要存EXE或者RAR等其它文件,要怎么改呢?
上面的TEXT版的测试成功,这里分享一下!
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.IO;
namespace FileToSql
{
public partial class FileToSql : Form
{
public FileToSql()
{
InitializeComponent();
}
Sqlite_helper SQL_hp = new Sqlite_helper();
private string buffer;
private void base64Encode(string fromFile)
{
FileStream fileStream = File.Open(fromFile, FileMode.Open);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
fileStream.Close();
this.buffer = Convert.ToBase64String(buffer);
}
private void base64Decode(string toFile , string buf)
{
FileStream fileStream = new FileStream(toFile, FileMode.Create);
byte[] buffer = Convert.FromBase64String(buf);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.base64Encode("D://AAA.exe");
SQL_hp.Sql_Execute("insert into FileToSql (ID,Name,File) values(3,'EXE','" + buffer + "')");
textBox1.Text = buffer;
}
private void button2_Click(object sender, EventArgs e)
{
this.base64Decode("D://aa//AAA.exe", SQL_hp.Sql_Row("select * from FileToSql WHERE ID = 3")[2].ToString());
}
}
}
—-
从数据存取出的JPG,EXE,RAR,要怎么写到D盘里呢?
—-
存到磁盘的话(在上面的读取代码中得到data之后:
using ( System.IO.FileStream file = new System.IO.FileStream(@"d:\doc\a.jpg", System.IO.FileMode.Create))
{
file.Write(data, 0, data.Length);
}
二进制文件存入数据库都用Blob类型。不过SQLite中的字段是无类型的,存入什么类型就是什么类型。注意我上面创建表的语句:
CREATE TABLE IMGS ( ID, IMAGE, PRIMARY KEY(ID))
这在其它数据库中是无法执行的。
当然在SQLite中同样也可以写成:
CREATE TABLE IMGS(ID NUMERIC, IMAGE BLOB, PRIMARY KEY(ID))
SQLite同样支持。事实上我现在都是这么写,因为已经习惯了。
—-
存取JPG图片成功了,请问怎么向数据库存EXE,RAR呢?
—-
CREATE TABLE IMGS(ID NUMERIC, IMAGE BLOB, PRIMARY KEY(ID)) 这个建表语句,如果数据库里己经有表了,就不用建了???我在代码里直接注释掉,应该没有问题吧!
—-
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())
// {
FileStream fileStream = File.Open("D://AA.exe", FileMode.Open);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
fileStream.Close();
//png.Save(strm, ImageFormat.Jpeg);
cmd.Parameters.Add(new SQLiteParameter("id_value", 4));
cmd.Parameters.Add(new SQLiteParameter("image_value", buffer.ToArray()));
cmd.ExecuteNonQuery();
// }
//}
}
}
谢谢,不好意思,对这些流转来转去还是第一次接触,乱搞搞出来了代码如上,应该没有错了!
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求rar,exe等各种文件存SQLITE 数据库的完整代码!