Chinaunix首页 | 论坛 | 博客
  • 博客访问: 223965
  • 博文数量: 45
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 915
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 16:03
文章分类

全部博文(45)

文章存档

2011年(1)

2008年(44)

我的朋友

分类:

2008-07-18 13:59:12

举例实现
一、创建accesss数据库
    建表
    create table imageTable(
       id 自动编号
       imageAAA  OLE对象
    )
建立好后,把access数据库放在工程目录的bin目录下的Debug目录中
 
二、创建frmAddImage窗体,实现写入图片操作
 
 

using System;
using System.Drawing;
using System.Text;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace _7788Demo
{
    ///



    /// frmAddImage 的摘要说明。

    ///


    public class frmAddImage : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox txtPicturePath;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.PictureBox imgPicture;
        private System.Windows.Forms.Label label2;
        ///

        /// 必需的设计器变量。

        ///


        private System.ComponentModel.Container components = null;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Button btnUpload;


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


        public frmAddImage()
        {
            //

            // Windows 窗体设计器支持所必需的

            //

            InitializeComponent();

            //

            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

            //

        }

        ///

        /// 清理所有正在使用的资源。

        ///


        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        ///

        /// 设计器支持所需的方法 - 不要使用代码编辑器修改

        /// 此方法的内容。

        ///


        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmAddImage));
            this.txtPicturePath = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.imgPicture = new System.Windows.Forms.PictureBox();
            this.label2 = new System.Windows.Forms.Label();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnUpload = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //

            // txtPicturePath

            //

            this.txtPicturePath.Location = new System.Drawing.Point(224, 164);
            this.txtPicturePath.Name = "txtPicturePath";
            this.txtPicturePath.Size = new System.Drawing.Size(192, 21);
            this.txtPicturePath.TabIndex = 13;
            this.txtPicturePath.Text = "";
            //

            // label1

            //

            this.label1.Location = new System.Drawing.Point(96, 84);
            this.label1.Name = "label1";
            this.label1.TabIndex = 12;
            this.label1.Text = "上传图片";
            //

            // imgPicture

            //

            this.imgPicture.Image = ((System.Drawing.Image)(resources.GetObject("imgPicture.Image")));
            this.imgPicture.Location = new System.Drawing.Point(224, 20);
            this.imgPicture.Name = "imgPicture";
            this.imgPicture.Size = new System.Drawing.Size(192, 120);
            this.imgPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.imgPicture.TabIndex = 11;
            this.imgPicture.TabStop = false;
            //

            // label2

            //

            this.label2.Location = new System.Drawing.Point(104, 164);
            this.label2.Name = "label2";
            this.label2.TabIndex = 16;
            this.label2.Text = "图片路径";
            //

            // btnAdd

            //

            this.btnAdd.Location = new System.Drawing.Point(440, 164);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.TabIndex = 15;
            this.btnAdd.Text = "添加";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            //

            // btnUpload

            //

            this.btnUpload.Location = new System.Drawing.Point(432, 76);
            this.btnUpload.Name = "btnUpload";
            this.btnUpload.TabIndex = 14;
            this.btnUpload.Text = "上传";
            this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
            //

            // frmAddImage

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(584, 262);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.btnUpload);
            this.Controls.Add(this.txtPicturePath);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.imgPicture);
            this.Name = "frmAddImage";
            this.Text = "添加图片";
            this.ResumeLayout(false);

        }
        #endregion

        //上传图片

        private void btnUpload_Click(object sender, System.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)
                {
                    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();                    
                }
            }
        }
        //添加图片操作,写入到数据库中

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            try
            {
                //数据库应为程序生成exe文件同目录

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

                OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.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());
            }
        }

        
    }
}

阅读(2158) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~