首 页文章中心下载中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:开源盛世-源代码下载网 -> 文章中心 -> 其他编程 -> VC# 技术 -> 文章内容 退出登录 用户管理
栏目导航
· VC# 技术 · Delphi技术
· Java技术 · 通用算法
· 编程相关
热门文章
· Tab Control控件使用...
· 学生档案管理系统
· [图文] 排列组合公式
· UTF-8与GB2312之间的...
· DirectShow下载安装...
· Virtual PC 在PAE模...
· Windows2000终端服务...
· MapInfo上的GIS系统...
· Mapbasic参考手册索...
· MapX应用开发中文讲...
相关文章
C# 读写 Oracle BLOB 数据
作者:不详  来源:vscodes.com整理  发布时间:2007-7-31 14:19:53  发布人:Polaris

减小字体 增大字体

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

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

        /**//*
         * 在调用此函数之前需要写插入一个字符串到 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) 处出错。
         */
        /**//// <summary>
        /// 文件写入到 Oracle Blob 字段中。
        /// </summary>
        /// <param name="idData">id 值</param>
        /// <param name="fileName">文件名</param>
        /// <param name="id">id 键</param>
        /// <param name="blob">blob 键</param>
        /// <param name="tableName">表名</param>
        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();
                }
            }
        }

        /**//// <summary>
        /// 读取 Oracle Blob 到文件中。
        /// </summary>
        /// <param name="idData">id 值</param>
        /// <param name="fileName">文件名</param>
        /// <param name="id">id 键</param>
        /// <param name="blob">blob 键</param>
        /// <param name="tableName">表名</param>
        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();
                }
            }
        }
    }
}

End of《C# 读写 Oracle BLOB 数据》

[] [返回上一页] [打 印] [收 藏]
 
∷相关“C# 读写 Oracle BLOB 数据”文章评论∷
(评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 网站目录 鄂ICP备06007162
开源盛世 版权所有Copyright © 2003-2005 VSCodes.Com. All Rights Reserved.