須要打開源碼
改一下數據源和表名還有字段名
sql
- 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
- {
-
-
-
-
-
-
-
- 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];
- file.Read(imgByte, 0, imgByte.Length);
- file.Close();
-
-
- cmd.CommandText = " insert into kk.kkzp ( xh,zp ) values ('17',:zp) ";
-
- 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];
- 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)
- {
- MemoryStream ms = new MemoryStream((byte[])dr["zp"]);
- Image imageBlob = Image.FromStream(ms, true);
- pictureBox1.Image = imageBlob;
- pictureBox1.Image.Save("d:\\abcc.jpg", pictureBox1.Image.RawFormat);
- }
- else
- {
- 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();
-
-
- 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())
- {
-
-
- 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.iteye.com");
- }
-
- #endregion
- }
- }