.net中二进制大对象的操作(这里以clob为例)

.net中二进制大对象的操作(这里以clob为例)

    public static void WriteLob(string table_name,string table_id,int id,int column,string content)
     {
       OracleConnection myCn = new OracleConnection(strConn);      
       OracleCommand myCmd = new OracleCommand();
       try
       {
         myCn.Open();        
       }
       catch(System.Data.OracleClient.OracleException e)
       {
         throw new Exception(e.Message);
       }      
       OracleTransaction myTrans = myCn.BeginTransaction();
       try
       {                      
         myCmd.Connection = myCn;        
         myCmd.Transaction = myTrans;
         myCmd.CommandText = "SELECT * FROM "+ table_name +" where "+ table_id +" = "+ id +" FOR UPDATE";
         OracleDataReader reader = myCmd.ExecuteReader();
         using(reader)
         {
           reader.Read();
           OracleLob clob = reader.GetOracleLob(column);//column为lob类型所在列
           Encoding UTF8 = Encoding.UTF8;                //使用UTF-8进行编码
           byte[] buffer  = UTF8.GetBytes(content);
           if(buffer.Length % 2 ==0)
             clob.Write(buffer, 0, buffer.Length);   //这里的参数必须为偶数
           else
             clob.Write(buffer, 0, (buffer.Length - 1));
           //clob.Position  = 0;
           myTrans.Commit();
         }
       }
       catch(System.Data.OracleClient.OracleException e)
       {      
         myTrans.Rollback();
         throw new Exception(e.Message);
       }
       finally
       {
         myCmd.Dispose();
         myCn.Close();
       }
     }
     public static string ReadLob(string table_name,string table_id,int id,int column)
     {
       OracleConnection myCn = new OracleConnection(strConn);      
       OracleCommand myCmd = new OracleCommand();
       try
       {
         myCn.Open();        
       }
       catch(System.Data.OracleClient.OracleException e)
       {
         throw new Exception(e.Message);
       }      
       OracleTransaction myTrans = myCn.BeginTransaction();
       try
       {                      
         myCmd.Connection = myCn;        
         myCmd.Transaction = myTrans;
         myCmd.CommandText = "SELECT * FROM "+ table_name +" where "+ table_id +" = "+ id +" FOR UPDATE";
         OracleDataReader reader = myCmd.ExecuteReader();
         using(reader)
         {
           reader.Read();
           OracleLob clob = reader.GetOracleLob(column);
           int[] content = new int[(int)clob.Length];
           byte[] buffer = new byte[(int)clob.Length];
             
           for(int i = 0;i<(int)clob.Length;i++)
           {
             content = clob.ReadByte();
             buffer  = (byte)content;
           }
 
           Encoding utf8 = Encoding.UTF8;
           
           string final = utf8.GetString(buffer);
           myTrans.Commit();
           return final;
         }
       }
       catch(System.Data.OracleClient.OracleException e)
       {      
         myTrans.Rollback();
         throw new Exception(e.Message);
       }
       finally
       {
         myCmd.Dispose();
         myCn.Close();
       }
     }
 
经测试通过,相信对有些朋友会有帮助的。

在读记录时
for(int i = 0;i<(int)clob.Length;i++)
           {
             content = clob.ReadByte();
             buffer  = (byte)content;
           }
读取太慢
用了clob.read(buffer,0,clob.length);
直接把记录读取buffer中,速度很快。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1728776

你可能感兴趣的:(.net)