将图片等文件保存到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.Xml
1.检查所要操作的xml文件是否存在:
System.IO.File.Exists(文件路径及名称);
2.得到xml文件:
(1)在asp.net中可以这样得到:
XmlDocument xmlDoc = new XmlDocument();
//导入xml文档
xmlDoc.Load( Server.MapPath("xmlTesting.xml"));
//导入字符串
/ ......
生成端处理
将要写入的值的前后写上如:"<![CDATA[" + string+ "]]>";
XmlNode xnformchild = doc.CreateNode(XmlNodeType.Element, dc.ColumnName.ToUpper(), "");
try
{
xnformchild.InnerXml = drform[dc.ColumnName].ToString( ......
无论在cmpp或sgip中,都会遇到短信编码格式转换的问题。
因为现在短信编码常用的格式有三种:0:ASCII串,8:UCS2编码,15:含GB汉字。在发送或接收短信时,都会用到相应的编码转换。如以下代码:
public static String Decode(Byte[] buf, int StartIndex, int Length, CODING Coding)
{
......
本文转载自 http://www.tntserver.cn/article.asp?id=41
今天遇到个问题.
首先插入一个DataTime格式的数据:
string sql="insert into [table] (date_time) values('" + date_time.ToString() + "'";
执行如上命令.插入没有报告错误.
但是,我又用一条命令读取时:
string sql="select * from [table];
.....
IDataRea ......