sql读写图片时Image.FromStream方法提示参数错误问题解决
来源:互联网 发布:电脑炒股软件 知乎 编辑:程序博客网 时间:2024/04/27 17:50
我们通常这么写
using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; }
我的写数据
private void btnOK_Click(object sender, EventArgs e) { string name = ""; if (tbxUserName.Text.Trim() == "") { MessageBox.Show("姓名不能为空,请重新输入!", "提示"); return; } else { name = tbxUserName.Text.Trim(); } string group = ""; if (cbxGroup.Text.Trim() == "") { group = "未分组"; } else { group = cbxGroup.Text.Trim(); } string phone = tbxTel.Text.Trim(); string workunit = tbxWorkUnit.Text.Trim(); string email = tbxEmail.Text.Trim(); string qq = tbxQQ.Text.Trim(); byte[] b = null; if (txtFilePath != "") { try { FileStream fs = new FileStream(txtFilePath, FileMode.Open, FileAccess.Read);//类型为打开数据 权限为只读 不呢写数据 int length = Convert.ToInt32(fs.Length); b = new byte[length]; fs.Read(b, 0, length);//数据读入b数组中 从0号到length位 fs.Close();//关闭输入输出流 } catch (Exception ex) { b = null; MessageBox.Show(ex.Message); } } else { b = pixData; } try { using (SqlConnection connection = new SqlConnection(connectionString)) { //获取当前的数据在表中的ID SqlCommand commandInsert = new SqlCommand(); commandInsert.Connection = connection; //connection.Close(); connection.Open(); //插入数据 commandInsert.CommandText = "USE db_TXL UPDATE tb_BusicInfo set Groups= @Groups ,Name=@Name,WorkUnit=@WorkUnit,Phone=@Phone,Email=@Email,QQ=@QQ,Picture=@Picture"; commandInsert.CommandText += " where ID=@ID "; commandInsert.Parameters.Add("@ID", SqlDbType.Int); commandInsert.Parameters.Add("@Groups", SqlDbType.VarChar, 50); commandInsert.Parameters.Add("@Name", SqlDbType.VarChar, 20); commandInsert.Parameters.Add("@WorkUnit", SqlDbType.VarChar, 50); commandInsert.Parameters.Add("@Phone", SqlDbType.VarChar, 14); commandInsert.Parameters.Add("@Email", SqlDbType.VarChar, 50); commandInsert.Parameters.Add("@QQ", SqlDbType.VarChar, 20); commandInsert.Parameters.Add("@Picture", SqlDbType.Image);//Image的类型不能写错!! commandInsert.Parameters["@ID"].Value = ID; // commandInsert.Parameters["@UserName"].Value = strUserName; commandInsert.Parameters["@Groups"].Value = group; commandInsert.Parameters["@Name"].Value = name; commandInsert.Parameters["@WorkUnit"].Value = workunit; commandInsert.Parameters["@Phone"].Value = phone; commandInsert.Parameters["@Email"].Value = email; commandInsert.Parameters["@QQ"].Value = qq; //commandInsert.Parameters[""].Value=; if (txtFilePath == "" && pixData==null) { commandInsert.Parameters["@Picture"].Value = DBNull.Value;//DBNull 不存在的值NULL } else { commandInsert.Parameters["@Picture"].Value = b; } commandInsert.ExecuteNonQuery(); connection.Close(); DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
我的读数据
private void frmEdit_Load(object sender, EventArgs e) { try { using (SqlConnection connection = new SqlConnection(connectionString)) { string commandString = "select * from tb_BusicInfo where ID= " + ID.ToString() + " "; SqlCommand command = new SqlCommand(commandString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { tbxUserName.Text = Convert.ToString(reader["Name"]); cbxGroup.Text = Convert.ToString(reader["Groups"]); tbxTel.Text = Convert.ToString(reader["Phone"]); tbxWorkUnit.Text = Convert.ToString(reader["WorkUnit"]); tbxEmail.Text = Convert.ToString(reader["Email"]); tbxQQ.Text = Convert.ToString(reader["QQ"]); if (reader["Picture"] == DBNull.Value) { pbxPicture.Image = TongXunLu.Properties.Resources.defaultPix; } else { // string a=reader["Picture"].ToString(); // byte[] b = (byte[])((reader["Picture"])); MemoryStream buf = new MemoryStream((byte[])reader["Picture"]); Image image = Image.FromStream(buf); Bitmap bt = new Bitmap(image); pbxPicture.Image = bt; //pbxPicture.Image = image; // pbxPicture.Image = Image.FromStream(new MemoryStream(b));//把二进制流读出来??问题 } } reader.Close(); connection.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
读数据的时候,但是在写数据的时候可能发生了一些错误。
果然我在追寻byte[]的数组的时候发现 写的时候有2万多个 读的时候只有50个
于是我想可能是写的时候出了问题,于是
commandInsert.Parameters.Add("@Picture", SqlDbType.Image,50);
改为commandInsert.Parameters.Add("@Picture", SqlDbType.Image);
照片可以正常显示了
参考了http://blog.csdn.net/zystory/article/details/4399338
你存数据的时候出了问题,和读数据没有关系
new SqlParameter("@L_RolePic", SqlDbType.Image, 16) 改为 new SqlParameter("@L_RolePic", SqlDbType.Image),
3 0
- sql读写图片时Image.FromStream方法提示参数错误问题解决
- sql读写图片时Image.FromStream 参数错误的问题解决
- 解决image.fromstream(Stream)参数无效的问题
- MVC Image.FromStream()转图片报错:内存不足
- PictureBox显示图片时提示参数错误
- 图片按钮提交问题解决方法 input image
- Image.FromStream与Image.FromFile
- Image::FromStream(pIStream)返回空
- Image::FromStream(pStream) 返回0
- Image.FromStream与Image.FromFile使用区别
- WPF Image问题解决方法
- Excel---八种常见Excel错误提示及问题解决方法
- Delphi COM+中调用webservice错误提示问题解决方法
- 读写SQL数据库Image字段
- 【Android】Universal Image Loader图片加载框架无法加载https图片问题解决方法
- PL/SQL提示无效的窗口句柄的问题解决方法
- phpcms会员中心上传图片,传递参数有错误提示解决办法
- 加载图片Image方法
- ir+关联规则
- SQL Server2008远程调用过程失败
- Linux下时间/时区的设置
- 一般筛法求素数+快速线性筛法求素数
- Android中proguard混淆第三方jar包
- sql读写图片时Image.FromStream方法提示参数错误问题解决
- 一切都只因为我太多情,才会不舍渐忘那些发霉的旧事
- freemarker获取系统信息,如操作系统信息,jvm版本信息等,tomcat的版本信息等
- LINUX Command: chown & chmod
- 使用RegisterNatives注册原生代码
- 【玩转微信公众平台之十】 图文消息回复讲解
- XML 实现 TextView的跑马灯效果
- android菜瓜笔记之手机照片选取与拍摄
- Boost[0]:环境搭建