首页 > 编程 > .NET > 正文

.net中二进制大对象的操作

2024-07-21 02:38:56
字体:
来源:转载
供稿:网友

  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();
      }
     }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表