我们通常这么写
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),
sql读写图片时Image.FromStream方法提示参数错误问题解决,布布扣,bubuko.com
sql读写图片时Image.FromStream方法提示参数错误问题解决
原文地址:http://blog.csdn.net/u010416101/article/details/26139033