在webform中把图片存到数据库

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

using System.IO;

 

namespace SendEMail

{

    public partial class frmSaveImg : Form

    {

 

        OleDbConnection conn;

        public frmSaveImg()

        {

            InitializeComponent();

            //OleDbConnection连接字符串

            string strConn = @"provider=Microsoft.Jet.OLEDB.4.0;data source=" + Application.StartupPath + "//db1.mdb";

            //创建OleDbConnection对象

            conn = new OleDbConnection(strConn);

        }

 

        /// <summary>

        /// 执行SQL语句函数

        /// </summary>

        /// <param name="sqlcmd">SQL语句</param>

        /// <param name="paras">SQL语句中的参数组</param>

        /// <returns>返回受影响记录条数</returns>

        public int ExecuteSql(string sqlcmd,params OleDbParameter[] paras)

        {

            OleDbCommand cmd = new OleDbCommand(sqlcmd, conn);

            if (conn.State == ConnectionState.Closed)

            {

                conn.Open();

            }

 

            foreach (OleDbParameter p in paras)

            {

                cmd.Parameters.Add(p);

            }

 

            int cnt = cmd.ExecuteNonQuery();

            conn.Close();

            return cnt;

        }

 

        /// <summary>

        /// 执行SQL查询

        /// </summary>

        /// <param name="sqlcmd">SQL语句</param>

        /// <returns>返回数据表</returns>

        public DataTable QuerySql(string sqlcmd)

        {

            OleDbDataAdapter oda = new OleDbDataAdapter(sqlcmd, conn);

            DataTable dt = new DataTable();

            oda.Fill(dt);

            return dt;

        }

 

        //单击pictureBox1是执行

        private void pictureBox1_Click(object sender, EventArgs e)

        {

            //打开文件对话框

            OpenFileDialog ofd = new OpenFileDialog();

            //选择图片后,点击确定按钮,加载图片

            if (ofd.ShowDialog() == DialogResult.OK)

            {

                pictureBox1.ImageLocation= ofd.FileName;

            }

        }

 

        //单击保存按钮执行图片保存到数据库中

        private void button1_Click(object sender, EventArgs e)

        {

            //插入数据SQL语句, img字段为表中存储图片的字段(ole类型)

            string sql = "insert into tb_img (img) values (@img)";

 

            //读取图片文件流

            FileStream fs = File.Open(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);

            //将流转化为byte数组

            byte[] MyData = new byte[fs.Length];

            fs.Read(MyData, 0, MyData.Length);

            fs.Close();

 

            //给SQL语句中的参数@img, 赋值

            OleDbParameter p = new OleDbParameter("@img", MyData);

            //执行SQL语句,将数据插入表中

            ExecuteSql(sql, p);

 

            //刷新comboBox1

            comboBox1.ValueMember = "id";

            comboBox1.DisplayMember = "id";

            comboBox1.DataSource = QuerySql("select id from tb_img");

        }

 

        //当comboBox1的index改变时执行

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            //得到当前comboBox1选中的记录

            DataTable dt = QuerySql("select * from tb_img where id =" + comboBox1.SelectedValue.ToString());

            //将值强转为byte数组

            byte[] MyData = (byte[])dt.Rows[0]["img"];

            //将byte[]数组写入到流中

            MemoryStream s = new MemoryStream();

            s.Write(MyData, 0, MyData.Length);

            //pictureBox1加载得到的流

            pictureBox1.Image = Image.FromStream(s);

        }

 

        //窗体启动时,comboBox1绑定数据库

        private void frmSaveImg_Load(object sender, EventArgs e)

        {

            comboBox1.ValueMember = "id";

            comboBox1.DisplayMember = "id";

            comboBox1.DataSource = QuerySql("select id from tb_img");

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值