zoukankan      html  css  js  c++  java
  • 数据库中插入和读取图片

    插入:picPath是图片的路径

    复制代码
    try             {                 //把照片通过流的方式读取到字节数组中!                FileStream fs = File.OpenRead(picPath);                 byte[] b =newbyte[fs.Length];                 fs.Read(b, 0, b.Length);
                    OleDbConnection con
    =new OleDbConnection(DB.connectionString);                 OleDbCommand cmd =new OleDbCommand("INSERT INTO Test (title,pic) VALUES (@title,@pic)", con);                 cmd.Parameters.Add("@title", OleDbType.VarChar).Value = txtTitle.Text;                 cmd.Parameters.Add("@pic", OleDbType.Binary).Value = b;
                    con.Open();                 cmd.ExecuteNonQuery();                 con.Close();
                    MessageBox.Show(
    "保存成功!");             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }
    复制代码

    读取:

    复制代码
    try             {                 string sql ="select pic from Test where Id = @Id";                 OleDbConnection con =new OleDbConnection(DB.connectionString);                 OleDbCommand cmd =new OleDbCommand(sql, con);                 cmd.Parameters.AddWithValue("@Id", txtId.Text);
                    con.Open();                
    byte[] b = (byte[])cmd.ExecuteScalar();                 con.Close();
                   
    if (b !=null)                 {                     MemoryStream ms =new MemoryStream(b);                     pictureBox1.Image = Image.FromStream(ms);                 }                 else                 {                     MessageBox.Show("未找到内容!");                 }             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }
    复制代码
  • 相关阅读:
    SpringMVC初写(二)映射类型、限制和数据绑定
    SpringMVC初写(一)SpringMVC的配置方式
    Spring框架初写
    CSS定位属性
    vuecli项目用yarn运行报错原因
    JS中出现NaN问题怎么解决?
    vue语法 `${ }` (模版字符串)
    Element-UI中Select选择器详解
    vue element-ui Radio单选框组件默认值选不中的原因:混用字符串和数字
    word-wrap属性允许长的内容可以自动换行
  • 原文地址:https://www.cnblogs.com/lovenan/p/3230281.html
Copyright © 2011-2022 走看看