winform保存二进制图片到oracle

數據庫實例  

2009-03-24 13:39:41|  分类: 默认分类 |举报|字号 订阅
  //圖片上傳
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"; //上傳格式

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fullpath = openFileDialog1.FileName;//文件路径 
                FileStream fs = new FileStream(fullpath, FileMode.Open);
                Byte[] imagebytes = new byte[fs.Length];  //二進制轉換
                BinaryReader br = new BinaryReader(fs);
                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length)); //讀取二進制流

                string cnnstr = "provider=OraOLEDB.Oracle;data source=testdb;User Id=hrerp;Password=hrerppwd;";
                OleDbConnection conn = new OleDbConnection(cnnstr);

                OleDbCommand cmd = new OleDbCommand(cnnstr, conn);
                conn.Open();


                string sql = "insert into hr_emp_attach(emp_no,emp_photo) values('000002',:photo)";
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Parameters.Add("photo", OleDbType.Binary,imagebytes.Length);
                cmd.Parameters[0].Value = imagebytes;
                cmd.ExecuteNonQuery();

                 MessageBox.Show("上传成功");
                conn.Close();
                 displayPhoto();//顯示圖片
            } 
        }

   public void displayPhoto()
        {
            string strSQL = "select emp_photo from hr_emp_attach where emp_no='000002'";
            string cnnstr = "provider=OraOLEDB.Oracle;data source=testdb;User Id=hrerp;Password=hrerppwd;";
            OleDbConnection con = new OleDbConnection(cnnstr);
            try
            {
                con.Open();
            }
            catch
            { }
            OleDbCommand cmd = new OleDbCommand(strSQL, con);
            System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr["emp_photo"] != DBNull.Value)//照片字段里有值才能进到方法体显示图片,否则清空pb      
                {
                    MemoryStream ms = new MemoryStream((byte[])dr["emp_photo"]);//把照片读到MemoryStream里      
                    Image imageBlob = Image.FromStream(ms, true);//用流创建Image      
                    pictureBox1.Image = imageBlob;//输出图片      
                    //pictureBox1.Image.Save("d:\\abcc.jpg", pictureBox1.Image.RawFormat); //將圖片保存起來
                }
                else//照片字段里没值,清空pb      
                {
                    pictureBox1.Image = null;
                }
            }  

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值