在C#中存储Blob类型的数据,
我现在在Oracle中写了个存储过程如下:
create or replace procedure update_student_clob(
v_cmid in number, --表的主键ID
v_geometry in blob --新的图形对象
)
is
lobloc blob;
query_str varchar2(1000);
begin
--取出blob对象
query_str :='select SPHOTOfrom student where STUDENTID= :id for update ';
EXECUTE IMMEDIATE query_str INTO lobloc USING v_cmid;
--更新
dbms_lob.write(lobloc, utl_raw.length(v_geometry),1, v_geometry);
commit;
end;
在C#中的代码部分如下:
1.数据层
public override void RunProcedure(string storedProcName,OracleParameter[] parameters)
{
cmd.CommandText=storedProcName;//声明存储过程名
cmd.CommandType=CommandType.StoredProcedure;
foreach(OracleParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();//执行存储过程
}
2.商业层
//用于存储照片,StuEntity 为实体类
public void AddStudent(StuEntity p_Entity)
{
DataAccess.DBOperator m_Ora = DBOperator.CreateKgInstance();
try
{
//fs为创建文件流
FileStream fs=new FileStream(p_Entity.photo,FileMode.OpenOrCreate);
//创建了二进制数组
byte[] blob = new byte[fs.Length];
fs.Read(blob, 0, blob.Length);
fs.Close();
//studid为主键
string m_Sql="update student set sphoto=empty_blob() where STUDENTID=1";
m_Ora.Execute(m_Sql);
SpExeFor(1,blob);
}
catch(Exception ex)
{
throw ex;
}
finally
{
m_Ora.Close();
}
}
public void SpExeFor(int p_Studid,byte[] p_Blob)
{
DataAccess.DBOperator m_Ora = DBOperator.CreateKgInstance();
try
{
//存储过程的参数声明
OracleParameter[] parameters={
new OracleParameter("v_cmid",OracleType.Int32),
new OracleParameter("v_geometry",OracleType.Blob,p_Blob.Length),
};
parameters[0].Value=p_Studid;
parameters[1].Value=p_Blob;
m_Ora.RunProcedure("update_student_clob",parameters);
}
catch(Exception e)
{
throw e;
}
finally
{
m_Ora.Close();
}
}
posted on 2005-04-19 14:38 surprise 阅读(15120) 评论(7) 编辑 收藏 举报