• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C# 实现SQLite数据库图片读写

武飞扬头像
ANDAIOT—Kxsong
帮助6

1、引用SQLite数据库库文件
学新通

2、编写文件读写类

//将图片数据转换为二进制流数据
private byte[] ImageToByte(Image Picture)
        {
            MemoryStream ms = new MemoryStream();
            if (Picture == null)
                return new byte[ms.Length];
            Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] BPicture = new byte[ms.Length];
            BPicture = ms.GetBuffer();
            return BPicture;
        }
//上传二进制流数据到数据库
        public void Updata_SQL(string FileName, byte[] picData)
        {
            SQLiteConnection conn = new SQLiteConnection("Data Source=D:\\ShowImage\\TestData.db; Version=3;");
            string sql = "";
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            // 直接返这个值放到数据就行了           
            sql = "Update ImageTable set ImageData = @Data where ImageName = "   FileName;
            cmd.CommandText = sql;
            cmd.Parameters.Add("@Data", DbType.Object, picData.Length);
            cmd.Parameters["@Data"].Value = picData;
            cmd.ExecuteNonQuery();
        }
//直接上传图片  内部自动转换为二进制流数据
        public void Updata_SQL(string FileName, Image Picture)
        {
            byte[] picData = ImageToByte(Picture);
            SQLiteConnection conn = new SQLiteConnection("Data Source=D:\\ShowImage\\TestData.db; Version=3;");
            string sql = "";
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            // 直接返这个值放到数据就行了           
            sql = "Update ImageTable set ImageData = @Data where ImageName = "   FileName;
            cmd.CommandText = sql;
            cmd.Parameters.Add("@Data", DbType.Object, picData.Length);
            cmd.Parameters["@Data"].Value = picData;
            cmd.ExecuteNonQuery();
        }
//读取数据库二进制流文件转图片
SQLiteConnection conn;           
            conn = new SQLiteConnection("Data Source=D:\\ShowImage\\TestData.db; Version=3;");
            conn.Open();
            string sql = "";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);              
            DataSet ds = new DataSet();
            sql = "select * from ImageTable";
            cmd.CommandText = sql;
            SQLiteDataAdapter adp = new SQLiteDataAdapter(cmd);
            adp.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];             
            conn.Close();
            pictureBox1.Image = Byte_Image(ds.Tables[0].Rows[0][1]);
            pictureBox2.Image = Byte_Image(ds.Tables[0].Rows[1][1]);
            pictureBox3.Image = Byte_Image(ds.Tables[0].Rows[2][1]);
            pictureBox4.Image = Byte_Image(ds.Tables[0].Rows[3][1]);
//二进制流转为图片方法
public Image Byte_Image(object value)
        {
            byte[] picData = (byte[])value;
            MemoryStream ms = new MemoryStream(picData);
            Image img = Image.FromStream(ms);
            ms.Close();
            return img;
        }
 //将文件读取转换为二进制流文件
  public byte[] GetFileBytes(string Filename)
        {

            if (Filename == "")
                return null;
            try
            {
                FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
                binaryReader.Close();
                return fileBytes;
            }
            catch
            {
                return null;
            }
        }
学新通

3、数据库设置
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfheeia
系列文章
更多 icon
同类精品
更多 icon
继续加载