C# 将存储在数据库中的图片获取到图片框
我正在尝试将存储在数据库中的图像恢复到图片框中C# 将存储在数据库中的图片获取到图片框,c#,sql,C#,Sql,我正在尝试将存储在数据库中的图像恢复到图片框中 SqlConnection conect = new SqlConnection("Data Source=DESKTOP-R34C6VV\\SQL;Initial Catalog=Restaurant;Integrated Security=True"); conect.Open(); string sql = "select Image from Menu where Name=@name"; SqlCommand cmd = new Sq
SqlConnection conect = new SqlConnection("Data Source=DESKTOP-R34C6VV\\SQL;Initial Catalog=Restaurant;Integrated Security=True");
conect.Open();
string sql = "select Image from Menu where Name=@name";
SqlCommand cmd = new SqlCommand(sql,conect);
cmd.Parameters.AddWithValue("@name",Invoice_combo.Text);
SqlDataReader read = cmd.ExecuteReader();
read.Read();
if (read.HasRows)
{
byte[] img = ((byte[])read[0]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream mystream = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(mystream);
cmd.Dispose();
}
}
else
{
MessageBox.Show("data not available");
}
conect.Close();
但是每次我运行这个程序,我都会在这一行上得到这个错误
pictureBox1.Image = System.Drawing.Image.FromStream(mystream);
其他信息:参数无效
当我搜索时,我看到很多人都有这个问题,但我没有找到任何解决问题的方法;我认为这是一个发生在初学者身上的问题
有人能帮我吗
这是我的数据库:
SELECT TOP 1000
[MenuID],
[Category],
[Name],
[Price],
[Image]
FROM
[Restaurant].[dbo].[Menu]
这是我用来插入图像的代码:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|ALL files(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
imglocation = dialog.FileName.ToString();
pictureBox1.ImageLocation = imglocation;
}
"Insert into Menu values ( 'someCategory','someItemname',42,'System.Byte[]')"
^
在“添加”按钮上
byte[] image = null;
FileStream stream = new FileStream(imglocation, FileMode.Open, FileAccess.Read);
BinaryReader brs = new BinaryReader(stream);
image = brs.ReadBytes((int)stream.Length);
String str = "Data Source=DESKTOP-R34C6VV\\SQL;Initial Catalog=Restaurant;Integrated Security=True";
String query1 = "Insert into Menu values ( '" + category + "','" + itemname + "'," + price + ",'"+image+"')";
String query2 = "Select MenuID from Menu where Name='" + itemname + "'";
SqlConnection con = null;
con = new SqlConnection(str);
SqlCommand cmd1 = new SqlCommand(query1, con);
SqlCommand cmd2 = new SqlCommand(query2, con);
SqlDataReader MyReader1;
SqlDataReader MyReader2;
con.Open();
MyReader1 = cmd1.ExecuteReader();
con.Close();
MessageBox.Show("Menu added successfully");
Thread.Sleep(2000);
con.Open();
MyReader2 = cmd2.ExecuteReader();
if (MyReader2.Read())
{
String id = System.Convert.ToString(MyReader2.GetInt32(0));
MessageBox.Show(itemname + " was given with MenuID - " + id);
}
con.Close();
完成else路径后,MemoryStream将脱离上下文。所以我想继续使用它将是一种未定义的行为。 将MemoryStream放入类成员变量中,并在应用程序结束之前
.Dispose()
另一方面,您的SqlCommand
没有得到正确处理。它只会在else{}块中被处理,这并不能保证发生。将它放入using语句中,围绕所有需要访问它以确保正确处理的内容。您的问题源于将图片保存到数据库的方式。生成insert命令时,将插入以下内容作为图像:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|ALL files(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
imglocation = dialog.FileName.ToString();
pictureBox1.ImageLocation = imglocation;
}
"Insert into Menu values ( 'someCategory','someItemname',42,'System.Byte[]')"
^
调用数组变量的ToString()
时,将获得数组的类型名称。因此,您没有将任何图像保存到数据库中-您只是反复保存相同的字符串
要解决此问题,请停止向SQL命令文本中注入值。请改用SQL参数:
var insertMenuQuery = "INSERT INTO Menu VALUES(@category, @itemName, @price, @image)";
SqlCommand cmd = new SqlCommand(insertMenuQuery, con);
cmd.Parameters.AddWithValue("@category", category);
cmd.Parameters.AddWithValue("@itemName", itemName);
cmd.Parameters.AddWithValue("@price", price);
cmd.Parameters.AddWithValue("@image", image);
// or cmd.Parameters.Add("@image", SqlDbType.VarBinary).Value = image;
这样,图像的适当二进制数据将存储在数据库中
注意:如果图像大于1MB,那么考虑使用FielestRAM代替ValDebug。< /P>如何保存图像到数据库?@ SergyByeZoVSKYI。我用我插入的代码更新了帖子,但不知道如何做第一部分,实际上我还是初学者,但我尝试了第二部分,我仍然有同样的错误,我会尝试搜索。第一部分谢谢