从数据库获取图片并显示(Oracle)
一、类
/**/
/// <summary>
/// 功能:获取附件
/// </summary>
/// <returns></returns>
public DataSet FileAttachmentGet( int intAttachmentID)
... {
OracleConnection oraConn;
OracleCommand oraCmd;
OracleParameter oraParm;
OracleDataAdapter oraAdapter;
DataSet ds;
oraConn = new OracleConnection ();
oraConn.ConnectionString = DBAccess.DBUtil.GetConnectionString();
oraConn.Open ();
oraCmd = new OracleCommand ();
oraCmd.CommandType = CommandType.StoredProcedure ;
oraCmd.CommandText ="pkg_TaskItem.up_FileAttachmentGet" ;
oraCmd.Connection = oraConn;
//附件ID
oraParm = new OracleParameter();
oraParm.OracleType = OracleType.Number;
oraParm.Direction = ParameterDirection.Input ;
oraParm.ParameterName = "AttachmentID";
oraParm.Value = intAttachmentID;
oraCmd.Parameters.Add(oraParm);
//结果集
oraParm = new OracleParameter();
oraParm.OracleType = OracleType.Cursor;
oraParm.Direction = ParameterDirection.Output;
oraParm.ParameterName = "objRs";
oraCmd.Parameters.Add(oraParm);
oraParm = null;
oraAdapter = new OracleDataAdapter();
oraAdapter.SelectCommand = oraCmd;
ds = new DataSet();
oraAdapter.Fill(ds);
oraAdapter = null;
oraCmd.Connection = null;
oraCmd = null;
oraConn.Close();
oraConn = null;
return ds;
}
/// 功能:获取附件
/// </summary>
/// <returns></returns>
public DataSet FileAttachmentGet( int intAttachmentID)
... {
OracleConnection oraConn;
OracleCommand oraCmd;
OracleParameter oraParm;
OracleDataAdapter oraAdapter;
DataSet ds;
oraConn = new OracleConnection ();
oraConn.ConnectionString = DBAccess.DBUtil.GetConnectionString();
oraConn.Open ();
oraCmd = new OracleCommand ();
oraCmd.CommandType = CommandType.StoredProcedure ;
oraCmd.CommandText ="pkg_TaskItem.up_FileAttachmentGet" ;
oraCmd.Connection = oraConn;
//附件ID
oraParm = new OracleParameter();
oraParm.OracleType = OracleType.Number;
oraParm.Direction = ParameterDirection.Input ;
oraParm.ParameterName = "AttachmentID";
oraParm.Value = intAttachmentID;
oraCmd.Parameters.Add(oraParm);
//结果集
oraParm = new OracleParameter();
oraParm.OracleType = OracleType.Cursor;
oraParm.Direction = ParameterDirection.Output;
oraParm.ParameterName = "objRs";
oraCmd.Parameters.Add(oraParm);
oraParm = null;
oraAdapter = new OracleDataAdapter();
oraAdapter.SelectCommand = oraCmd;
ds = new DataSet();
oraAdapter.Fill(ds);
oraAdapter = null;
oraCmd.Connection = null;
oraCmd = null;
oraConn.Close();
oraConn = null;
return ds;
}
二、页面
/**/
/// <summary>
/// 功能:获取图片
/// </summary>
private void GetImage()
... {
Archive.Components.ErrorImage objImg = new PM.Archive.Components.ErrorImage();
DataSet ds = null;
byte[] ImageBuffer = null ;
string strFileName = "";
string strUrl = "";
string strFileType = "";
try
...{
int intAttachmentID = Convert.ToInt32( GetQueryString("AttachmentID"));
ds = objImg.FileAttachmentGet(intAttachmentID);
int n = ds.Tables[0].Rows.Count;
if(ds != null && ds.Tables[0].Rows.Count > 0 )
...{
ImageBuffer =(byte []) ds.Tables[0].Rows[0]["f_FileContent"];
strFileType = ds.Tables[0].Rows[0]["f_FileType"].ToString();
strFileName = ds.Tables[0].Rows[0]["f_FileName"].ToString();
Response.Clear();
Response.ContentType = strFileType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName));
Response.BinaryWrite(ImageBuffer);
Response.Flush();
Response.End();
}
else
...{
strUrl = "../Images/NoErrorView.gif";
Response.Redirect(strUrl,false);
}
}
catch(Exception err)
...{
MessageOracleShow(err.Message.ToString());
}
}
/// 功能:获取图片
/// </summary>
private void GetImage()
... {
Archive.Components.ErrorImage objImg = new PM.Archive.Components.ErrorImage();
DataSet ds = null;
byte[] ImageBuffer = null ;
string strFileName = "";
string strUrl = "";
string strFileType = "";
try
...{
int intAttachmentID = Convert.ToInt32( GetQueryString("AttachmentID"));
ds = objImg.FileAttachmentGet(intAttachmentID);
int n = ds.Tables[0].Rows.Count;
if(ds != null && ds.Tables[0].Rows.Count > 0 )
...{
ImageBuffer =(byte []) ds.Tables[0].Rows[0]["f_FileContent"];
strFileType = ds.Tables[0].Rows[0]["f_FileType"].ToString();
strFileName = ds.Tables[0].Rows[0]["f_FileName"].ToString();
Response.Clear();
Response.ContentType = strFileType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName));
Response.BinaryWrite(ImageBuffer);
Response.Flush();
Response.End();
}
else
...{
strUrl = "../Images/NoErrorView.gif";
Response.Redirect(strUrl,false);
}
}
catch(Exception err)
...{
MessageOracleShow(err.Message.ToString());
}
}
引用<IMG alt="描述图片" src='<%# DataBinder.Eval(Container, "DataItem.f_AttachmentID", "ImageGet.aspx?AttachmentID={0}") %>' align="middle">
三、存储过程
--
******************************************************************
-- 功能:附件的获取
-- ******************************************************************
PROCEDURE up_FileAttachmentGet
(
AttachmentID NUMBER ,
objRs OUT curRecordset
)
IS
BEGIN
OPEN objRs FOR
SELECT *
FROM vw_FileAttachment
WHERE f_AttachmentID = AttachmentID;
END up_FileAttachmentGet;
-- 功能:附件的获取
-- ******************************************************************
PROCEDURE up_FileAttachmentGet
(
AttachmentID NUMBER ,
objRs OUT curRecordset
)
IS
BEGIN
OPEN objRs FOR
SELECT *
FROM vw_FileAttachment
WHERE f_AttachmentID = AttachmentID;
END up_FileAttachmentGet;