using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.IO;
using My.DB;
using Me.Data;
namespace Blob
{
public partial class Form1 : Form
{
private OracleConnection conn;
public Form1()
{
InitializeComponent();
}
private OracleLob GetTempBLobs(RichTextBox rb) //獲取提交資料庫的檔內容
{
//輸入檔內容
//Stream stream = Stream.Null;
//OpenFileDialog openFileDialog = new OpenFileDialog();
//string fileName = openFileDialog.FileName;
//openFileDialog.CheckFileExists = true;
//if (openFileDialog.ShowDialog() == DialogResult.OK)
//{
// stream = openFileDialog.OpenFile();
//}
//else
//{
// return OracleLob.Null;
//}
//stream.Position =0 ;
byte[] byte_buffer = null;
//buffer=System.Text.Encoding.Default.GetBytes(
byte_buffer = System.Text.Encoding.ASCII.GetBytes(rb.Text); // Encoding.ASCII.GetBytes("xxxxx");
//byte[] byte_buffer = new byte[stream.Length];
//for(int i=0;i 0)
{
//buffer = (byte)dt.Rows[0]["comments"].ToString();
//OracleCommand cmd = PublicData.db..CreateCommand();
OracleCommand cmd = PublicData.OraconnSFC.CreateCommand();
string sqlstr = "select " + blobfield + " from " + tablename + " where " + fieldone + "='" + valueone + "' and " + fieldtwo + "='" + valuetwo + "'";
cmd.CommandText = sqlstr;
OracleDataReader dr = cmd.ExecuteReader();
//OracleDataReader dr = PublicData.db.ExecuteDataReader(Read, op);
while (dr.Read())
{
blob = dr.GetOracleLob(0);
}
}
else
return;
}
//打開保存內容的檔
string filename = string.Empty;
byte[] buffer = null;
buffer = new byte[blob.Length];
blob.Read(buffer, 0, buffer.Length);
richTextBox1.AppendText(System.Text.Encoding.Default.GetString(buffer));
//System.Text.Encoding.Default.GetString
//System.Text.Encoding.Default.GetBytes
#region //保存成文件的代碼
//*****************保存成文件的代碼*************//
//將內容寫入檔---考慮怎樣將文件寫入richeedit控件
//SaveFileDialog saveFileDialog = new SaveFileDialog();
//if (saveFileDialog.ShowDialog() == DialogResult.OK)
//{
// filename = saveFileDialog.FileName;
//}
//if (filename == string.Empty)
//{
// MessageBox.Show("請確定保存的檔路徑!");
// return;
//}
//將Blob類型的內容轉化成檔流類型
//richTextBox1.AppendText(System.Text.Encoding.Default.GetString(buffer));
//FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
//fs.Write(buffer, 0, buffer.Length);
//fs.Close();
#endregion
}
public void UpdateBlob(string tablename, string blobfield, string fieldone, string fieldtwo,string valueone, string valuetwo,RichTextBox rbt)//更新Blob內容
{
//提交更新的檔內容
OracleLob blob = OracleLob.Null;
//Stream stream = Stream.Null; //把保存文件的註釋掉了
//OpenFileDialog openFileDialog = new OpenFileDialog();
//string fileName = openFileDialog.FileName;
//openFileDialog.CheckFileExists = true;
//if (openFileDialog.ShowDialog() == DialogResult.OK)
//{
// stream = openFileDialog.OpenFile();
//}
//else
//{
// return;
//}
//獲取要更新的資料庫內容
OracleTransaction transaction = PublicData.OraconnSFC.BeginTransaction();
try
{
OracleCommand cmd = PublicData.OraconnSFC.CreateCommand();
cmd.Transaction = transaction;
string Update = "select " + blobfield + " from " + tablename + " where " + fieldone + "='" + valueone + "' and " + fieldtwo + "='" + valuetwo + "' FOR update";
cmd.CommandText = Update;
//MessageBox.Show(Update);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
blob = dr.GetOracleLob(0);
}
}
catch (Exception ex)
{
throw(ex);
}
//向資料庫寫入更新內容
//stream.Position =0;
//byte[] byte_buffer = new byte[stream.Length];
//for(int i=0;i 0)
// {
// tempLob.Write(Buffer, 0, i);
// }
// fs.Close();
// tempLob.EndBatch();
// cmd.Parameters.Clear();
// }
// // 提交事務
// transaction.Commit();
// }
// catch (Exception ex)
// {
// throw ex;
// }
// finally
// {
// conn.Close();
// }
// }
//}
///
/// 讀取 Oracle Blob 到文件中。,然後打開文件
///id 值
///檔案名
///id 鍵
///blob 鍵
///表名
//public void ReadBlob(string idData,string fileName, string id, string blob, string tableName)
//{
// string connString = "Password=notenote456z;Persist Security Info=True;User ID=note;Data Source=test";
// using(conn = new OracleConnection(connString))
// {
// try
// {
// conn.Open();
// OracleCommand cmd = conn.CreateCommand();
// // 利用事務處理(必須)
// OracleTransaction transaction = cmd.Connection.BeginTransaction();
// cmd.Transaction = transaction;
// // 獲得 OracleLob 指針
// string sql = "select " + blob + " from " + tableName + " where " + id + " = " + idData;
// cmd.CommandText = sql;
// OracleDataReader dr = cmd.ExecuteReader();
// dr.Read();
// OracleLob tempLob = dr.GetOracleLob(0);
// dr.Close();
// // 讀取 BLOB 中數據,寫入到文件中
// FileStream fs = new FileStream(fileName,FileMode.Create);
// int length = 1048576;
// byte[] Buffer = new byte[length];
// int i;
// while((i = tempLob.Read(Buffer,0,length)) > 0)
// {
// fs.Write(Buffer,0,i);
// }
// fs.Close();
// tempLob.Clone();
// cmd.Parameters.Clear();
// // 提交事務
// transaction.Commit();
// }
// catch(Exception ex)
// {
// throw ex;
// }
// finally
// {
// conn.Close();
// }
// }
//}
#endregion 不用的方法
} //Class
} //Namepace