//上传代码
 int len = FileUpload1.PostedFile.ContentLength;
     byte[] pic = new byte[len];
    FileUpload1.PostedFile.InputStream.Read(pic, 0, len);
    Insert the image and comment into the database    string  strImageType = FileUpload1.PostedFile.ContentType;
    MySqlConnection connection = DAL.DBHelp.Conn();
     try
        {
          MySqlCommand cmd = new MySqlCommand("update tbl_resume set c_photo='"+pic+"' where c_uid='aaa111'",connection);  
          cmd.ExecuteNonQuery();
        }
        finally
        {
            connection.Close();
        }
//显示
            MemoryStream stream = new MemoryStream();
            MySqlConnection connection = DAL.DBHelp.Conn();
          try
          {
               MySqlCommand command = new MySqlCommand("select c_photo from tbl_resume where c_uid='aaa111'", connection);              byte[] image = (byte[])command.ExecuteScalar();
              stream.Write(image, 0, image.Length);
              Bitmap bitmap = new Bitmap(stream);
              Response.ContentType = "image/gif";
              bitmap.Save(Response.OutputStream, ImageFormat.Gif);
          }
          finally
          {
              connection.Close();
              stream.Close();
          }显示时报错
用户代码未处理 System.ArgumentException
  Message="参数无效。"
  Source="System.Drawing"
  StackTrace:
       在 System.Drawing.Bitmap..ctor(Stream stream)
       在 Default2.Page_Load(Object sender, EventArgs e) 位置 e:\GibbsCAMCertificate\GibbsCAMCertificate\Default2.aspx.cs:行号 34
       在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       在 System.Web.UI.Control.OnLoad(EventArgs e)
       在 System.Web.UI.Control.LoadRecursive()
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
数据库字段  c_photo 为 blob 类型 。但不知道为什么保存的byte[]长度要比实际在上传时读出的byte[]小很多。
在上传跟踪时发现byte[100523]   但是在数据库中却只有byte[13]   不知道是在哪出错了。
还有就是同样的方法保存在mssql中就可以正常显示,但在MYSQL中就不行

解决方案 »

  1.   

    在表中存储二进制数据时应该使用存储过程或者参数化SQL语句。
      

  2.   

    在ADO.NET中使用参数化SQL语句的大同小异 
    在ADO.NET中经常需要跟各种数据库打交道,在不实用存储过程的情况下,使用参数化SQL语句一定程度上可以防止SQL注入,同时对一些较难赋值的字段(如在SQL Server中Image字段,在Oracle中Clob字段等)使用参数化SQL语句很容易就能赋值,所以本人经常在ADO.NET中使用参数化SQL语句,近几年来陆续跟SQL Server/Oracle/ MySQL/Access打交道,积累了一些心得,现在整理出来供大家参考。  
      

  3.   

    你那个插入图片数据应该不会想数据库中插入数据,不信你打开看看。用下面的办法试试。
    MySqlCommand cmd = new MySqlCommand("update tbl_resume set c_photo=?c_photo where c_uid='aaa111'",connection); 
    cmd.Paramters.AddWithValue("?c_photo",pic);
              cmd.ExecuteNonQuery();
      

  4.   

    我这是在测试    我找到了在mysql中是用blob类型    但是不知道为什么保存的时候数据变小了   所以显示不了
      

  5.   

    问题解决了    因为我在MYSQL中 c_photo 的字读类型为bolb   所以可能是存储的长度不够    现在改成longblob然后再用
    MySqlCommand cmd = new MySqlCommand("update tbl_resume set c_photo=?c_photo where c_uid='aaa111'",connection); 
    cmd.Paramters.AddWithValue("?c_photo",pic);
              cmd.ExecuteNonQuery();   就可以正常显示了