C# 读写 Oracle BLOB 数据

using System;
using System.Data.OracleClient;
using System.IO;

namespace fenghua.Data.Oracle
{
  ///
   /// Class1 的摘要说明。
   ///
   public class OracleLobData
   {
       private OracleConnection conn;
       public OracleLobData()
       {
           //
           // TODO: 在此处添加构造函数逻辑
           //
       }

       /*
       * 在调用此函数之前需要写插入一个字符串到 BLOB 中比如:
        * Select some data.
        * Table Schema:
        *        "CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)";
        *        "INSERT INTO tablewithlobs values (1, 'AA', 'AAA', N'AAAA')";
        * 否则程序会在 OracleLob tempLob    = reader.GetOracleLob(0) 处出错。
        */
       ///
       /// 文件写入到 Oracle Blob 字段中。
       ///
       /// id 值
       /// 文件名
       /// id 键
       /// blob 键
       /// 表名
       public void WriteBlob(int idData, string fileName, string id, string blob, string tableName)
       {
           string connString = "server=oratest;User ID=kttest;Password=test";
           using(conn = new OracleConnection(connString))
           {
               try
               {
                   conn.Open();
                   OracleCommand cmd = conn.CreateCommand();

                   // 利用事务处理(必须)
                   OracleTransaction transaction = cmd.Connection.BeginTransaction();
                   cmd.Transaction = transaction;

                   // 获得 OracleLob 指针
                   cmd.CommandText = "select " + blob + " from " + tableName + " where " + id + " = " + idData + " FOR UPDATE";
                   OracleDataReader reader = cmd.ExecuteReader();
                   using(reader)
                   {
                       //Obtain the first row of data.
                       reader.Read();
                       //Obtain a LOB.
                       OracleLob tempLob    = reader.GetOracleLob(0);

                       // 将文件写入 BLOB 中
                       FileStream fs = new FileStream(fileName,FileMode.Open);
                       tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
                       int length = 10485760;
                       byte[] Buffer = new byte[length];
                       int i;
                       while((i = fs.Read(Buffer,0,length)) > 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(int idData,string fileName, string id, string blob, string tableName)
       {
           string connString = "server=oratest;User ID=kttest;Password=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();
               }
           }
       }
   }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值