需要打开源码
改一下数据源和表名还有字段名
改一下数据源和表名还有字段名
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Data.OleDb;
- namespace ImageSaveOracleBlobDataBase
- {
- public partial class Form1 : Form
- {
- /// <summary>
- /// 作者:黑色头发
- /// C# WinForm操作oracle数据库的Blob字段
- /// blog:http://heisetoufa.javaeye.com
- /// 静网:http://www.heisetoufa.cn
- /// </summary>
- public Form1()
- {
- InitializeComponent();
- }
- #region 保存图片到oracle数据库
- private void button1_Click(object sender, EventArgs e)
- {
- string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
- OleDbConnection con = new OleDbConnection(cnnstr);
- try
- {
- con.Open();
- }
- catch
- { }
- OleDbCommand cmd = new OleDbCommand(cnnstr, con);
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = cnnstr;
- string imgPath = @"d:\aa\a.jpg";//图片文件所在路径
- FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
- Byte[] imgByte = new Byte[file.Length];//把图片转成 Byte型 二进制流
- file.Read(imgByte, 0, imgByte.Length);//把二进制流读入缓冲区
- file.Close();
- cmd.CommandText = " insert into kk.kkzp ( xh,zp ) values ('17',:zp) ";//正常sql语句插入数据库
- cmd.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, imgByte.Length);
- cmd.Parameters[0].Value = imgByte;
- try
- {
- cmd.ExecuteNonQuery();
- MessageBox.Show("插入成功");
- }
- catch (System.Exception e1)
- {
- MessageBox.Show(e1.Message);
- }
- }
- #endregion
- #region 修改图片
- private void button2_Click(object sender, EventArgs e)
- {
- string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
- OleDbConnection con = new OleDbConnection(cnnstr);
- string imgPath = @"d:\aa\b.jpg";//图片文件所在路径
- FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
- Byte[] ddd = new Byte[file.Length];//把图片转成 Byte型 二进制流
- file.Read(ddd, 0, ddd.Length);//把二进制流读入缓冲区
- file.Close();
- string strSQL = "update kk.kkzp set zp=:zp where xh = '16' ";
- try
- {
- con.Open();
- }
- catch
- { }
- OleDbCommand cmd = new OleDbCommand(cnnstr, con);
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = strSQL;
- cmd.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, ddd.Length).Value = ddd;
- cmd.ExecuteNonQuery();
- MessageBox.Show("修改成功");
- }
- #endregion
- #region 读出数据库里图片
- private void button3_Click(object sender, EventArgs e)
- {
- string strSQL = "select xh,zp from kk.kkzp where xh = 16 ";
- string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
- 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["zp"] != DBNull.Value)//照片字段里有值才能进到方法体显示图片,否则清空pb
- {
- MemoryStream ms = new MemoryStream((byte[])dr["zp"]);//把照片读到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;
- }
- }
- }
- #endregion
- #region 使文本编辑变得可见
- private void button4_Click(object sender, EventArgs e)
- {
- textBox1.Visible = true;
- button5.Visible = true;
- button6.Visible = true;
- button7.Visible = true;
- }
- #endregion
- #region 保存文本
- private void button5_Click(object sender, EventArgs e)
- {
- string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
- OleDbConnection con = new OleDbConnection(cnnstr);
- try
- {
- con.Open();
- }
- catch
- { }
- OleDbCommand cmd = new OleDbCommand(cnnstr, con);
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = cnnstr;
- string txvalue = this.textBox1.Text.Trim();
- //将textBox1.Text转换为二进制类型
- byte[] expbyte = System.Text.Encoding.Unicode.GetBytes(txvalue);
- cmd.CommandText = " insert into kk.kkzp ( xh,zp ) values ('18',:zp) ";
- cmd.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, expbyte.Length);
- cmd.Parameters[0].Value = expbyte;
- try
- {
- cmd.ExecuteNonQuery();
- MessageBox.Show("插入结束");
- }
- catch (System.Exception e1)
- {
- MessageBox.Show(e1.Message);
- }
- }
- #endregion
- #region 修改文本
- private void button6_Click(object sender, EventArgs e)
- {
- string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
- OleDbConnection con = new OleDbConnection(cnnstr);
- byte[] ddd;
- ddd = System.Text.Encoding.Unicode.GetBytes(this.textBox1.Text);
- string strSQL = "update kk.kkzp set zp=:zp where xh = 18 ";
- try
- {
- con.Open();
- }
- catch
- { }
- OleDbCommand cmd = new OleDbCommand(cnnstr, con);
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = strSQL;
- cmd.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, ddd.Length).Value = ddd;
- cmd.ExecuteNonQuery();
- MessageBox.Show("OK!");
- }
- #endregion
- #region 读出文本
- private void button7_Click(object sender, EventArgs e)
- {
- string strSQL = "select xh,zp from kk.kkzp where xh = 18 ";
- string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
- 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())
- {
- //读取BLOB数据时也要将其转换回文本类型
- string dd = dr["zp"].ToString();
- byte[] ooo = (byte[])dr["zp"];
- string str;
- str = System.Text.Encoding.Unicode.GetString(ooo);
- this.textBox1.Text = str;
- }
- }
- #endregion
- #region 退出
- private void button8_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- #endregion
- #region 单击状态栏
- private void toolStripStatusLabel2_Click(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start("http://heisetoufa.javaeye.com");
- }
- #endregion
- }
- }