乐逍遥

博客园 首页 新随笔 联系 订阅 管理

 /// <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();
   }
  }

posted on 2007-07-10 10:31  乐逍遥  阅读(815)  评论(4编辑  收藏  举报