如何在c#程序中调用Oracle存储过程上传二进制文件?
最新做一个新的项目,用到了微软的Enterprise Library库,该库支持SQL和Oracle的统一访问,因此不能用SqlConnectionClient或OracleConnectionClient中自定义的数据类型,只能使用通用的DbType,但是其中的二进制内容只能容纳8K,这对于上传文件或者图片来说,肯定是不够用的,通过仔细研究Oracle的存储过程,自己写了一个无限上传二进制内容的存储过程:
createorreplaceprocedureP_UpdatePhoto(v_imginblob,v_eraseFlaginvarchar2,v_employee_idinvarchar2)is
v_locator_varblob;
v_amountinteger:=0;
begin
selectPHOTOintov_locator_varfromemployeeswhereemployee_id=v_employee_idFORUPDATE;
IF(v_locator_varISNULL)THEN
UPDATEEMPLOYEESSETPHOTO=EMPTY_BLOB()WHEREEMPLOYEE_ID=v_employee_id;
selectPHOTOintov_locator_varfromemployeeswhereemployee_id=v_employee_idFORUPDATE;
ENDIF;
if(v_locator_varisnotnullandv_eraseFlag='1')then
v_amount:=DBMS_LOB.getlength(v_locator_var);
IF(v_amount!=0)THEN
DBMS_LOB.erase(v_locator_var,v_amount,1);
DBMS_LOB.trim(v_locator_var,0);
ENDIF;
endif;
DBMS_LOB.append(v_locator_var,v_img);
commit;
endP_UpdatePhoto;
v_locator_varblob;
v_amountinteger:=0;
begin
selectPHOTOintov_locator_varfromemployeeswhereemployee_id=v_employee_idFORUPDATE;
IF(v_locator_varISNULL)THEN
UPDATEEMPLOYEESSETPHOTO=EMPTY_BLOB()WHEREEMPLOYEE_ID=v_employee_id;
selectPHOTOintov_locator_varfromemployeeswhereemployee_id=v_employee_idFORUPDATE;
ENDIF;
if(v_locator_varisnotnullandv_eraseFlag='1')then
v_amount:=DBMS_LOB.getlength(v_locator_var);
IF(v_amount!=0)THEN
DBMS_LOB.erase(v_locator_var,v_amount,1);
DBMS_LOB.trim(v_locator_var,0);
ENDIF;
endif;
DBMS_LOB.append(v_locator_var,v_img);
commit;
endP_UpdatePhoto;
然后在程序中调用就简单了,封装一个函数
注:数据访问接口需要Enterprise Library的支持
///<summary>
///上传二进制文件
///</summary>
///<paramname="empId">员工工号</param>
///<paramname="fullName">二进制文件的本地全路径</param>
publicvoidUploadPhoto(stringempId,stringfullName)
{
if(empId.Trim().Length==0||!File.Exists(fullName))
thrownewArgumentException("员工工号为空或者指定的图片文件不存在");
constintIMG_SIZE=7900;
try
{
System.Collections.ArrayListarrParameters=newSystem.Collections.ArrayList();
FileStreamF_Stream=newFileStream(fullName,FileMode.Open,FileAccess.Read);
Byte[]ImageData=newbyte[F_Stream.Length];
F_Stream.Read(ImageData,0,(int)F_Stream.Length);
F_Stream.Position=0;
intsize=ImageData.Length%IMG_SIZE==0?ImageData.Length/IMG_SIZE:ImageData.Length/IMG_SIZE+1;
CCBPServices.Parameterprm_eraseFlag=null,prm_employee_id=null,prm_photo=null;
stringstrErr=string.Empty;
try
{
for(inti=0;i<size;i++)
{
arrParameters.Clear();
prm_eraseFlag=newCCBPServices.Parameter(true,"v_eraseFlag",System.Data.DbType.String,1);
prm_employee_id=newCCBPServices.Parameter(true,"v_employee_id",System.Data.DbType.String,10);
prm_employee_id.realValue=empId;
if(i==0)
prm_eraseFlag.realValue="1";
else
prm_eraseFlag.realValue="0";
Byte[]b=null;
if(i==size-1)
b=newByte[ImageData.Length-i*IMG_SIZE];
else
b=newByte[IMG_SIZE];
F_Stream.Read(b,0,b.Length);
prm_photo=newCCBPServices.Parameter(true,"v_img",System.Data.DbType.Binary,b.Length);
prm_photo.realValue=b;
//prm_photo.realValue=b;
arrParameters.Add(prm_photo);
arrParameters.Add(prm_eraseFlag);
arrParameters.Add(prm_employee_id);
Queryquery=newQuery(CommandType.StoredProcedure,"P_UpdatePhoto",arrParameters,string.Empty);
ExcecuteStoredProc(refquery,refstrErr);
}
}
catch
{}
F_Stream.Close();
}
catch(Exceptionex)
{
throwex;
}
}
///上传二进制文件
///</summary>
///<paramname="empId">员工工号</param>
///<paramname="fullName">二进制文件的本地全路径</param>
publicvoidUploadPhoto(stringempId,stringfullName)
{
if(empId.Trim().Length==0||!File.Exists(fullName))
thrownewArgumentException("员工工号为空或者指定的图片文件不存在");
constintIMG_SIZE=7900;
try
{
System.Collections.ArrayListarrParameters=newSystem.Collections.ArrayList();
FileStreamF_Stream=newFileStream(fullName,FileMode.Open,FileAccess.Read);
Byte[]ImageData=newbyte[F_Stream.Length];
F_Stream.Read(ImageData,0,(int)F_Stream.Length);
F_Stream.Position=0;
intsize=ImageData.Length%IMG_SIZE==0?ImageData.Length/IMG_SIZE:ImageData.Length/IMG_SIZE+1;
CCBPServices.Parameterprm_eraseFlag=null,prm_employee_id=null,prm_photo=null;
stringstrErr=string.Empty;
try
{
for(inti=0;i<size;i++)
{
arrParameters.Clear();
prm_eraseFlag=newCCBPServices.Parameter(true,"v_eraseFlag",System.Data.DbType.String,1);
prm_employee_id=newCCBPServices.Parameter(true,"v_employee_id",System.Data.DbType.String,10);
prm_employee_id.realValue=empId;
if(i==0)
prm_eraseFlag.realValue="1";
else
prm_eraseFlag.realValue="0";
Byte[]b=null;
if(i==size-1)
b=newByte[ImageData.Length-i*IMG_SIZE];
else
b=newByte[IMG_SIZE];
F_Stream.Read(b,0,b.Length);
prm_photo=newCCBPServices.Parameter(true,"v_img",System.Data.DbType.Binary,b.Length);
prm_photo.realValue=b;
//prm_photo.realValue=b;
arrParameters.Add(prm_photo);
arrParameters.Add(prm_eraseFlag);
arrParameters.Add(prm_employee_id);
Queryquery=newQuery(CommandType.StoredProcedure,"P_UpdatePhoto",arrParameters,string.Empty);
ExcecuteStoredProc(refquery,refstrErr);
}
}
catch
{}
F_Stream.Close();
}
catch(Exceptionex)
{
throwex;
}
}
本文地址:http://www.45fan.com/a/question/69971.html