将图片保存到ORACEL数据库和读取数据显示到页面

来源:互联网 发布:淘宝站外活动网站 编辑:程序博客网 时间:2024/05/14 02:49

一.读取客户端文件,返回Byte[]

        /// <summary>
        
/// 读取客户端图片文件
        
/// </summary>
        
/// <param name="input">HtmlInputFile控件</param>
        
/// <returns>返回文件的Byte值</returns>

        private byte[] UpLoadFile(HtmlInputFile input,TextBox txtPhotoPath)
        
{
            
byte[] btImage = null;

            
if(input.Value != String.Empty && input.Value == txtPhotoPath.Text)
            
{
                
try
                
{
                    System.IO.Stream streamImage 
= input.PostedFile.InputStream ;
                    btImage 
= new byte[streamImage.Length];
                    streamImage.Read(btImage,
0,(int)streamImage.Length);
                }

                
catch(Exception e)
                
{
                    
throw new Exception(e.Message,e);
                }

            }


            
return btImage; 
        }

 

二.添加到ORACLE数据

        /// <summary>
        
/// 添加图片至数据库
        
/// </summary>
        
/// <param name="btImge">图片二进制数据</param>
        
/// <returns></returns>

        public string GetUpdateSQL(Byte[] btImge)
        
{
            
string UpdateSql = null;
            Database db 
= DatabaseFactory.CreateDatabase();

            
using (IDbConnection conn = db.GetConnection())
            
{            
                    
                DBCommandWrapper dbCmd 
= db.GetStoredProcCommandWrapper(sqlMag.GetSQL("getupdatesql"));

                                                dbCmd.AddParameter(
"Image",OracleType.LongRaw,btImage.Length,ParameterDirection.Input,
                                                    
true,0,0,"FileText",DataRowVersion.Default,btImge);        
                                                                                    db.ExecuteNonQuery(dbCmd);
            }

        }

 

三.在页面上显示

    /// <summary>
  /// 取得用户照片信息
  /// </summary>
  private void GetStaffPhoto()
  {
   //从数据库中取得数据
   DBDycnamicCreateControls dyc = new DBDycnamicCreateControls(_sourceId,1);
   DataSet ds = dyc.QuerySingleRecordData(_recordId);

   try
   {
    //存为内存流
    Stream streamImg = new MemoryStream((byte[])ds.Tables[0].Rows[0][_fieldName]);
    //保存到图片中
    System.Drawing.Bitmap imgPhoto = new System.Drawing.Bitmap(streamImg,true);
    //保存到位图中,并输出到页面
    imgPhoto.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
   }
   catch(Exception ee)
   {
    throw new Exception(ee.Message,ee);
   }
  }