大数据类型通过存储过程保存数据(clob,blob)

今天终于把通过 存储过程传递和保存大数据类型的问题给解决了.在网上找了好多的资料,最试了都不行.最后还是按照微软的msdn上的例子参照自己的理解写了个执行存储过程的函数.

/// <summary>
  /// 执行带有clob,blob,nclob大对象参数类型的存储过程
  /// </summary>
  /// <param name="storedProcName">存储过程名称</param>
  /// <param name="parameters">存储过程参数</param>
  /// <param name="index">大对象在参数中的位置</param>
  /// <param name="tempbuff">大对象的值</param>
  /// <param name="DBType">大对象具体类型</param>
  public void RunProcedure(string storedProcName, IDataParameter[] parameters,int[] index,byte[][]tempbuff,OracleType[] DBType)
  {  
   
   Connection.Open();
   OracleTransaction tx = Connection.BeginTransaction();
   OracleCommand cmd = Connection.CreateCommand();
   cmd.Transaction = tx;
   string type=" declare ";
   for(int i=0;i< DBType.Length;i++)
   {
    type=type+" xx"+i.ToString()+" "+DBType[i].ToString()+";";
   }
   string createtemp=type+" begin ";
   for(int i=0;i< DBType.Length;i++)
   {
    createtemp=createtemp+" dbms_lob.createtemporary(xx"+i.ToString()+", false, 0); ";
   }
   string setvalue="";
   for(int i=0;i< DBType.Length;i++)
   {
    setvalue=setvalue+":templob"+i.ToString()+" := xx"+i.ToString()+";";
   }

   cmd.CommandText =createtemp+setvalue+" end;";

   for(int i=0;i< DBType.Length;i++)
   {
    cmd.Parameters.Add(new OracleParameter("templob"+i.ToString(), DBType[i])).Direction = ParameterDirection.Output;
   }
   cmd.ExecuteNonQuery();
   
   

   int j=0;
   foreach(int i in index)
   {
    OracleLob tempLob = (OracleLob)cmd.Parameters["templob"+j.ToString()].Value;
    tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
    int abc=tempbuff[j].Length;
    if(DBType[j]==OracleType.Clob||DBType[j]==OracleType.NClob)
    {
     double b=abc/2;
     double a=Math.Ceiling(b);
     abc=(int)(a*2);
    }
    tempLob.Write(tempbuff[j],0,abc);
    tempLob.EndBatch();
    parameters[i].Value=tempLob;
    j++;
    
   }

   cmd.Parameters.Clear();

   cmd.CommandText = storedProcName;
   
   cmd.CommandType = CommandType.StoredProcedure;

   foreach (OracleParameter parameter in parameters)
   {
    cmd.Parameters.Add( parameter);
   
   }
   cmd.ExecuteNonQuery(); 
   tx.Commit();
   
   Connection.Close();  
   
  }

可以支持一个存储过程包含多个大对象类型参数.

在存储过程中保存数据可以这样写

PROCEDURE document_create
 (
  strdocname     VARCHAR2,
   strdocdirid number,
  strdoctime   date,
  strdoccontect  clob,
    p_new blob
    ) AS
   tmp_id number;
   lobloc   blob;
   tmp_clob clob;
 BEGIN
  select sq_strdocid.Nextval into tmp_id from dual;
  INSERT INTO document
   (doc_id,doc_name,doc_dir_id,doc_time,doc_contect,news)
  VALUES
   (tmp_id,strdocname,strdocdirid,strdoctime,empty_clob(),empty_blob());
     
      select news into lobloc from document where doc_id=tmp_id for update;
     
      DBMS_LOB.copy(lobloc,p_new,DBMS_LOB.getlength(p_new));
    --  DBMS_LOB.write(lobloc,DBMS_LOB.getlength(p_new),1,p_new);
     
       select doc_contect into tmp_clob from document where doc_id=tmp_id for update;
      DBMS_LOB.copy(tmp_clob,strdoccontect,DBMS_LOB.getlength(strdoccontect));
   --   DBMS_LOB.write(tmp_clob,DBMS_LOB.getlength(strdoccontect),1,strdoccontect);
 END document_create;

数据足够大的时候也可以保存数据...

但我一直有一个疑问,开始的时候我使用DBMS_LOB.write来保存数据,如果数据太大就报错,不知道是什么原因,后来改成DBMS_LOB.Copy就没有问题,,,不知道哪为大虾知道为什么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值