将图片等文件保存到sqlite中(c#)
SqLite.net的dll为System.Data.SQLite.dll,这种dll分为32位、64位和适用于compactframework三种,在引用时要注意,选择正确的dll。
将要保存图片的字段类型设为blob。代码如下:
private void savePicture()
{
using (SQLiteConnection cnn = new SQLiteConnection(dbPath))
{
cnn.Open();
using (SQLiteCommand cmd = cnn.CreateCommand())
{
//cmd.CommandText = "Create Table test(data Image)";
//cmd.ExecuteNonQuery();
cmd.CommandText = "insert into person values('12',@data,'14','13')";
SQLiteParameter para = new SQLiteParameter("@data", DbType.Binary);
string file = @"F:\Image\飞机.png";
FileStream fs = new FileStream(file, FileMode.Open);
//StreamUtil su = new StreamUtil();
//byte[] buffer = su.StreamToBytes(fs);
byte[] buffer = StreamUtil.ReadFully(fs);
fs.Close();
para.Value = buffer;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
}
}
}
其中StreamUtil为自定义的一个类:
public static class StreamUtil
{
const int BufferSize = 8192;
public static void CopyTo(Stream input,Stream output)
{
byte[] buffer = new byte[BufferSize];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
public static byte[] ReadFully(Stream input)
{
using (MemoryStream tempStream = new MemoryStream())
{
CopyTo(input, tempStream);
return tempStream.ToArray();
}
}
}
参考:http://www.
相关文档:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace pub.mo
{
public class request
{
private request() { }
/// <summary>
/// 获取session
/// </summary>
/// <param name="_session_name" ......
前一阵字做项目(嵌入式linux),由于要保存大量的数据,而且最长要保存30天的时间。本来打算保存到文件中,每次启动应用程序的时候重新解析一遍,可是当数据量很大的时候,就出现效率的问题了。所以最后还是放弃了使用文件的打算,决定使用数据库存取数据。
linux下的数据库也很多,有开源的,也有收费的。对于我们来说,肯 ......
2010这个元旦太无聊了,于是找了个奇迹游戏私服耍,在玩的过程中发现总是要登录到网站上去转生加点之类的操作,1次2次还好,100次你就郁闷了,于是自己想写个简单的程序来做。
以下就是代码部分啦:
(需要注意的是WebClient的Cookie部分)
using System;
using System.Collections.Generic;
using System.ComponentMo ......