当前位置:编程学习 > C#/ASP.NET >>

如何在winform 里面把图片保存在数据库里,从数据库(access)里显示在窗体上?

如何在winform 里面把图片保存在数据库(access)里,从数据库(access)里显示在窗体上?需要详细代码。 --------------------编程问答-------------------- 学习。。 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;

namespace DemoText
{
    public partial class UpLoadPicture : Form
    {
        public UpLoadPicture()
        {
            InitializeComponent();
        }

        private byte[] image; //数据存储声明byte[]数组

        private void butUpLoad_Click(object sender, EventArgs e)
        {
             //图片上传对话框
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "浏览";
            ofd.Filter = "JPEG|*.jpeg;*.jpg|Bitmap|*.bmp;*.dib|GIF|*.gif;*.gfa|Portable Network Graphics|*.png|Windows Metafile|*.wmf|Windows Enhanced Metafile|*.emf";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (ofd.FileName != null)
                {
                    try
                    {
                        this.imgPicture.Image = Image.FromFile(ofd.FileName);
                        this.txtPicturePath.Text = ofd.FileName; //上传图片路径

                        //转换为byte类型,保存

                        MemoryStream ms = new MemoryStream();
                        this.imgPicture.Image.Save(ms, imgPicture.Image.RawFormat);
                        image = null;
                        image = ms.ToArray(); //赋值byte                    
                        ms.Close();
                    }
                    catch
                    { 
                    
                    }
                }
            }

        }

        private void butOk_Click(object sender, EventArgs e)
        {

            try
            {
                //数据库应为程序生成exe文件同目录

                string path = Application.StartupPath + "\\ImageDB.accdb"; //取得数据库所在目录

                //连接2003
                //OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "'");

                //连接2007
                OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" + path + "'");
                
                string strSql = "Insert into imageTable(imageAAA) values(@imageAAA)";
                OleDbCommand cmd = new OleDbCommand(strSql, oConn);
                //一定要用这种方式设置参数添加

                cmd.Parameters.Add("@imageAAA", image);
                //判断连接是否打开

                if (oConn.State == ConnectionState.Closed)
                {
                    oConn.Open();
                }
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    MessageBox.Show("添加成功", "信息提示");
                }
                oConn.Close();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message.ToString());
            }
        }

    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,