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

    技术2022-05-11  114

    今天终于把通过 存储过程传递和保存大数据类型的问题给解决了.在网上找了好多的资料,最试了都不行.最后还是按照微软的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就没有问题,,,不知道哪为大虾知道为什么


    最新回复(0)