C#操作Blob類型數據技術

本文介绍了一种使用C#进行Oracle数据库Blob字段读写的方法,包括获取、更新Blob内容及将其转换为文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄贤秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值