将图片等文件保存到sqlite中(c#)

将图片等文件保存到sqlite中(c#),第1张

概述sqli.net的dll为System.Data.SQLite.dll,这种dll分为32位、64位和适用于compactframework三种,在引用时要注意,选择正确的dll。 将要保存图片的字段类型设为blob。代码如下:         private void savePicture() { using (SQLiteConnection cnn

sqli.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,buffer.Length)) > 0)            {                output.Write(buffer,read);            }         }                  public static byte[] ReadFully(Stream input)              {               using (MemoryStream tempStream = new MemoryStream())            {                copyTo(input,tempStream);                return tempStream.ToArray();            }        }         }

参考:http://www.kaiyuan8.org/Article/qfuoQyWKDicoYpoirorz.aspx
C#教程:声明和调用扩展方法:http://www.webjx.com/aspnet/2009-04-12/11229.html


http://topic.csdn.net/u/20081024/09/9b2bf0ad-ec15-4b00-9994-3124038ba329.html

该方法主要是利用了 sqliteParameter 的功能,读取blob字段。代码如下:

fileStream m_filestream = null;    try {    m_filestream = new fileStream(@"d:\pcinfo.jpg",fileMode.Open,fileAccess.Read); //读取图片 sqliteCommand m_commd2=new sqliteCommand(); m_commd2.CommandText="UPDATE test1 set timage=@IDimage WHERE tparendID=78";    Byte[] m_byte = new Byte[m_filestream.Length]; //存放图片 m_filestream.Read(m_byte,m_byte.Length); m_filestream.Close(); sqliteParameter param_m=new sqliteParameter("@IDimage",DbType.Binary,m_byte.Length,ParameterDirection.input,false,null,DaTarowVersion.Current,m_byte); m_commd2.Parameters.Add(param_m); m_commd2.Parameters.Add(param_m); //很多参数阿,注意DBType.Binary    m_commd2.Connection = m_conn; m_commd2.ExecuteNonquery();    } catch (sqliteException ex) { MessageBox.Show("未能存入图片");    }
总结

以上是内存溢出为你收集整理的将图片等文件保存到sqlite中(c#)全部内容,希望文章能够帮你解决将图片等文件保存到sqlite中(c#)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/sjk/1174753.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-02
下一篇2022-06-02

发表评论

登录后才能评论

评论列表(0条)

    保存