(原) ODP.NET 演示以流的形式来获取 LOB 数据
 using System;
using System; using System.Data;
using System.Data; using System.Text;
using System.Text; using Oracle.DataAccess.Client;
using Oracle.DataAccess.Client; using Oracle.DataAccess.Types;
using Oracle.DataAccess.Types; using System.IO;
using System.IO;
 namespace Sample44
namespace Sample44 {
{ // 演示以流的形式来获取 LOB 数据
    // 演示以流的形式来获取 LOB 数据 class Program
    class Program {
    { static void Main(string[] args)
        static void Main(string[] args) {
        { // Connect
            // Connect string constr = "User Id=scott;Password=tiger;Data Source=bjoracle";
            string constr = "User Id=scott;Password=tiger;Data Source=bjoracle"; OracleConnection conn = Connect(constr);
            OracleConnection conn = Connect(constr);
 // Setup
            // Setup Setup(conn);
            Setup(conn);
 //Set the command
            //Set the command OracleCommand cmd = new OracleCommand("select story from multimedia_tab where thekey=1");
            OracleCommand cmd = new OracleCommand("select story from multimedia_tab where thekey=1"); cmd.Connection = conn;
            cmd.Connection = conn; cmd.CommandType = CommandType.Text;
            cmd.CommandType = CommandType.Text;
 // Execute
            // Execute OracleDataReader reader;
            OracleDataReader reader; try
            try {
            { // Create DataReader
                // Create DataReader reader = cmd.ExecuteReader();
                reader = cmd.ExecuteReader();
 //Read the first row
                //Read the first row while (reader.Read())
                while (reader.Read()) {
                { // Set the OracleClob object to the CLOB selected
                    // Set the OracleClob object to the CLOB selected OracleClob clob = reader.GetOracleClob(0);
                    OracleClob clob = reader.GetOracleClob(0);
 // Read data all data
                    // Read data all data System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
                    System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
 //byte[] clob_data = new byte[1024];
                    //byte[] clob_data = new byte[1024]; Int64 amountRead = 0;
                    Int64 amountRead = 0; int readSize = 8;
                    int readSize = 8; Int64 totalRead = 0;
                    Int64 totalRead = 0;
 //do
                    //do //{
                    //{ //    amountRead = clob.Read(clob_data, (int)totalRead, readSize);
                    //    amountRead = clob.Read(clob_data, (int)totalRead, readSize); //    Console.WriteLine("Actual read: {0} bytes", amountRead);
                    //    Console.WriteLine("Actual read: {0} bytes", amountRead); //    Console.WriteLine("Current: {0}", converter.GetString(clob_data));
                    //    Console.WriteLine("Current: {0}", converter.GetString(clob_data)); //    totalRead += amountRead;
                    //    totalRead += amountRead; //} while (amountRead > 0);
                    //} while (amountRead > 0);
 string str = string.Empty;
                    string str = string.Empty; do
                    do {
                    { byte[] clob_data = new byte[8];
                        byte[] clob_data = new byte[8]; amountRead = clob.Read(clob_data, 0, readSize);
                        amountRead = clob.Read(clob_data, 0, readSize); str+=converter.GetString(clob_data);
                        str+=converter.GetString(clob_data); totalRead += amountRead;
                        totalRead += amountRead; } while (amountRead > 0);
                    } while (amountRead > 0);
 Console.WriteLine("Total number of bytes read: {0}", totalRead);
                    Console.WriteLine("Total number of bytes read: {0}", totalRead); Console.WriteLine("Story: {0}", str);
                    Console.WriteLine("Story: {0}", str); // Dispose OracleClob object
                    // Dispose OracleClob object clob.Dispose();
                    clob.Dispose(); }
                } }
            } catch (Exception ex)
            catch (Exception ex) {
            { Console.WriteLine("Error: {0}", ex.Message);
                Console.WriteLine("Error: {0}", ex.Message); }
            } finally
            finally {
            { // Dispose OracleCommand object
                // Dispose OracleCommand object cmd.Dispose();
                cmd.Dispose();
 // Close and Dispose OracleConnection object
                // Close and Dispose OracleConnection object conn.Close();
                conn.Close(); conn.Dispose();
                conn.Dispose(); }
            }
 // Waiting
            // Waiting Console.ReadLine();
            Console.ReadLine(); }
        }
 /// <summary>
        /// <summary> /// 打开数据库连接
        /// 打开数据库连接 /// </summary>
        /// </summary> /// <param name="connectStr"></param>
        /// <param name="connectStr"></param> /// <returns></returns>
        /// <returns></returns> public static OracleConnection Connect(string connectStr)
        public static OracleConnection Connect(string connectStr) {
        { OracleConnection conn = new OracleConnection(connectStr);
            OracleConnection conn = new OracleConnection(connectStr); try
            try {
            { conn.Open();
                conn.Open(); }
            } catch (Exception ex)
            catch (Exception ex) {
            { Console.WriteLine("Error: {0}", ex.Message);
                Console.WriteLine("Error: {0}", ex.Message); }
            } return conn;
            return conn; }
        }
 /// <summary>
        /// <summary> /// 创建必要的表和测试数据
        /// 创建必要的表和测试数据 /// </summary>
        /// </summary> /// <param name="conn"></param>
        /// <param name="conn"></param> public static void Setup(OracleConnection conn)
        public static void Setup(OracleConnection conn) {
        { StringBuilder blr;
            StringBuilder blr; OracleCommand cmd = new OracleCommand("", conn);
            OracleCommand cmd = new OracleCommand("", conn);
 blr = new StringBuilder();
            blr = new StringBuilder(); blr.Append("DROP TABLE multimedia_tab");
            blr.Append("DROP TABLE multimedia_tab"); cmd.CommandText = blr.ToString();
            cmd.CommandText = blr.ToString(); try
            try {
            { cmd.ExecuteNonQuery();
                cmd.ExecuteNonQuery(); }
            } catch (Exception ex)
            catch (Exception ex) {
            { Console.WriteLine("Error: {0}", ex.Message);
                Console.WriteLine("Error: {0}", ex.Message); }
            }
 blr = new StringBuilder();
            blr = new StringBuilder(); blr.Append("CREATE TABLE multimedia_tab(thekey NUMBER(4) PRIMARY KEY,");
            blr.Append("CREATE TABLE multimedia_tab(thekey NUMBER(4) PRIMARY KEY,"); blr.Append("story CLOB, sound BLOB)");
            blr.Append("story CLOB, sound BLOB)"); cmd.CommandText = blr.ToString();
            cmd.CommandText = blr.ToString(); try
            try {
            { cmd.ExecuteNonQuery();
                cmd.ExecuteNonQuery(); }
            } catch (Exception ex)
            catch (Exception ex) {
            { Console.WriteLine("Error: {0}", ex.Message);
                Console.WriteLine("Error: {0}", ex.Message); }
            }
 // 插入大对象
            // 插入大对象 string str = string.Empty;
            string str = string.Empty; using (StreamReader tr = new StreamReader(@"C:\arcgis9.2\tt.txt"))
            using (StreamReader tr = new StreamReader(@"C:\arcgis9.2\tt.txt")) {
            { str=tr.ReadToEnd();
                str=tr.ReadToEnd(); }
            }
 blr = new StringBuilder();
            blr = new StringBuilder(); blr.Append("INSERT INTO multimedia_tab values(");
            blr.Append("INSERT INTO multimedia_tab values("); blr.Append("1,");
            blr.Append("1,"); blr.Append(":story,");
            blr.Append(":story,"); blr.Append("'123456789545454545454545454554545454545454')");
            blr.Append("'123456789545454545454545454554545454545454')"); cmd.CommandText = blr.ToString();
            cmd.CommandText = blr.ToString(); //cmd.CommandText = "EXECMULT";
            //cmd.CommandText = "EXECMULT"; //cmd.CommandType = CommandType.StoredProcedure;
            //cmd.CommandType = CommandType.StoredProcedure;
 OracleParameter param = new OracleParameter("story", OracleDbType.Clob);
            OracleParameter param = new OracleParameter("story", OracleDbType.Clob); param.Direction = ParameterDirection.Input;
            param.Direction = ParameterDirection.Input; param.Value = str;
            param.Value = str; cmd.Parameters.Add(param);
            cmd.Parameters.Add(param);
 try
            try {
            { cmd.ExecuteNonQuery();
                cmd.ExecuteNonQuery(); }
            } catch (Exception ex)
            catch (Exception ex) {
            { Console.WriteLine("Error: {0}", ex.Message);
                Console.WriteLine("Error: {0}", ex.Message); }
            } finally
            finally {
            { cmd.Dispose();
                cmd.Dispose(); str = null;
                str = null;
 }
            } }
        } }
    } }
}
1、从数据库中取大数据时,要一部分一部分的取,然后存入存储设备中,这样可以节省内存的消耗。
2、在插入 clob 类型的数据时,要看数据量的大小,当小于2000字符时,可以直接插入,否则参与参数的形式插入。
多于2000字符出现 ORA-01704:文字字符串过长 的错误,解决方案见:http://www.cnblogs.com/mjgforever/archive/2007/12/27/1016515.html
posted on 2007-12-27 10:51 mjgforever 阅读(417) 评论(0) 收藏 举报
 
                    
                     
                    
                 
                    
                 
 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号