大數據類型通過存儲過程保存數據(clob,blob)
今天終於把通過 存儲過程傳遞和保存大數據類型的問題給解決了.在網上找了好多的資料,最試了都不行.最後還是按照微軟的msdn上的例子參照自己的理解寫了個執行存儲過程的函數.
///
/// 執行帶有clob,blob,nclob大對象參數類型的存儲過程
///
/// 存儲過程名稱
/// 存儲過程參數
/// 大對象在參數中的位置
/// 大對象的值
/// 大對象具體類型
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就沒有問題,,,不知道哪爲大蝦知道爲什麼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.