参数无效。将数据库中的图像输入图片框 [英] Parameter is not valid. Getting image from database into picture box
问题描述
这是我将图片从picturebox插入数据库的代码。
Image img = this.pictureBox1.Image;
byte [] arr;
ImageConverter converter = new ImageConverter();
arr =(byte [])converter.ConvertTo(img,typeof(byte []));
然后我将arr插入数据库。
这是我用来从数据库中选择图像到图片框的代码。但它给了我一个错误参数无效。
string query =SELECT * FROM Drug WHERE DrugCode ='+ this.DataGridView1.CurrentRow.Cells [0] .Value +';
SqlCommand cmd = new SqlCommand(query,Conet.con);
SqlDataReader row = cmd.ExecuteReader();
Form_EditDrug newMDIChild = new Form_EditDrug(this.txtStaff.Text);
newMDIChild.TopMost = true;
newMDIChild .BringToFront();
if(row.Read())
{
byte [] data =(byte [])row [ 图片];
MemoryStream ms = new MemoryStream(data);
newMDIChild.pictureBox1.Image = Image.FromStream(ms);
}
请帮助。
This is my code for inserting image from picturebox into database.
Image img = this.pictureBox1.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
I then insert arr into the database.
This is the code I use to select the image from database into a picturebox. But it gives me an error"Parameter is not valid."
string query = "SELECT * FROM Drug WHERE DrugCode = '" + this.DataGridView1.CurrentRow.Cells[0].Value + "'";
SqlCommand cmd = new SqlCommand(query, Conet.con);
SqlDataReader row = cmd.ExecuteReader();
Form_EditDrug newMDIChild = new Form_EditDrug(this.txtStaff.Text);
newMDIChild.TopMost = true;
newMDIChild.BringToFront();
if (row.Read())
{
byte[] data = (byte[])row["Picture"];
MemoryStream ms = new MemoryStream(data);
newMDIChild.pictureBox1.Image = Image.FromStream(ms);
}
Help please.
推荐答案
见这里:为什么我得到参数无效 。我从数据库中读取图像时出现异常? [ ^ ]
请不要进行数据库访问办法!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
And please, don't do database access that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
这篇关于参数无效。将数据库中的图像输入图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!