//上传代码
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中就不行
解决方案 »
- 无法将类型“System.Data.SqlClient.SqlCommand”隐式转换为“SqlDataReader”
- ocx在winform下封装成dll,再从webform下引用,在虚拟主机下出现"访问拒绝"???
- ASP.NET网站布署不上去啊!
- 请教个跟地区挂钩的权限问题?
- 如何动态释放内存(C#)
- 请教论坛中所有高手!我的问题我觉得很复杂,希望大家能帮我!
- 这个错误怎么改阿? “ 在 SQL 语句结尾之后找到字符”
- 急问一个SQL语句问题
- 关于类型转换
- 比较急,高分求解简单文件~
- C#怎么从图文混排的源代码中,提取图片地址。。
- 如何在C#中获取HTML控件的值和如果在前台JS中获取服务器控件的值?
在ADO.NET中经常需要跟各种数据库打交道,在不实用存储过程的情况下,使用参数化SQL语句一定程度上可以防止SQL注入,同时对一些较难赋值的字段(如在SQL Server中Image字段,在Oracle中Clob字段等)使用参数化SQL语句很容易就能赋值,所以本人经常在ADO.NET中使用参数化SQL语句,近几年来陆续跟SQL Server/Oracle/ MySQL/Access打交道,积累了一些心得,现在整理出来供大家参考。
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();
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(); 就可以正常显示了