把图片保存到数据库的实现

 /// <summary>
  /// 将照片转换为二进制数组
  /// </summary>
  /// <param name="path"></param>
  /// <returns></returns>
  private byte[] PhotoToArray( string path )
  {
   FileStream stream = new FileStream( path , FileMode.Open , FileAccess.Read ) ;
   byte[] bufferPhoto =new byte[stream.Length] ;
   stream.Read( bufferPhoto,0,Convert.ToInt32( stream.Length ) ) ;
   stream.Flush();
   stream.Close();

   return bufferPhoto ;
  }

  //把二进制的图片插到数据库
private void Save(byte[] image)
  {
   string sql = "insert into table2(aaa,photo) values(@aaa,@photo)";
   SqlParameter[] param=new SqlParameter[2];
   param[0] =new SqlParameter("@aaa",SqlDbType.Int);
   param[0].Value = 1;
   param[1]= new SqlParameter("@photo",SqlDbType.Image);
   param[1].Value= image;

   SqlConnection conn= new SqlConnection();
   conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[0];

   SqlCommand commd= new SqlCommand(sql,conn);
   commd.Parameters.Add(param[0]);
   commd.Parameters.Add(param[1]);

   try
   {
    conn.Open();
    commd.ExecuteNonQuery();
    MessageBox.Show("把图片成功的插入数据库");
   }
   catch(Exception ex)
   {
    throw ex;
   }
   finally
   {
    conn.Close();
   }
  }

转载于:https://www.cnblogs.com/zhcx/archive/2007/07/10/812261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值