在C#winform应用程序中将Image从MySql加载到PictureBox2时,参数无效



请帮助我,因为我已经尝试了来自多个来源的许多解决方案。我有一个pictureBox2,下面有一个显示按钮。这个想法是,如果我单击显示按钮,JPEG图像将出现在我的MySql数据库的pictureBox2中。该图像位于一个名为Recopy的表中的blob字段中(请原谅我对配方的拼写错误(。这是我的代码:

MySqlCommand comm;
MySqlDataReader rdr;
MySqlDataAdapter da;
string Query = "select image from chefassist.recipy where name ='" + textBox1.Text + "';";   
MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid 
= root; pwd = n0clu387;");
comm = new MySqlCommand(Query, connectToD);
da = new MySqlDataAdapter(comm);
try
{
connectToD.Open();

rdr = comm.ExecuteReader();
while(rdr.Read())
{
//string sName = rdr.GetString("name");
//textBox1.Text = sName;
byte[] imgg = (byte[])(rdr["image"]);
if (imgg == null)
{
pictureBox2.Image = null;
}
else
{
MemoryStream mstream = new MemoryStream(imgg);
pictureBox2.Image = System.Drawing.Image.FromStream(mstream);
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

首先,您应该在SQL查询中使用参数

string Query = "select image from chefassist.recipy where name = @Param;   

using( MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid = root; pwd = n0clu387;"))
{
using( comm = new MySqlCommand(Query, connectToD))
{
com.Parameters.AddWithValue("@Param", textBox1.Text); --More secure like this.
//da = new MySqlDataReader(comm); --I didn't understand why you need this. Use 
DataReader instead, your code below seems using a datareader.
try
{
connectToD.Open();

//If you are sure that the query will return only one image, you can use 
// byte[] buffer = (byte[])(comm.ExecuteScalar());

//If you want to use a reader
var rdr = comm.ExecuteReader();
while(rdr.Read())
{

//string sName = rdr.GetString("name");
//textBox1.Text = sName;
byte[] buffer = (byte[])(rdr["image"]);
if (imgg == null)
{
pictureBox2.Image = null;
}
else
{
pictureBox2.Image = ByteArrayToImage(buffer);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

你可以尝试这种方法来转换你的字节数组yo图像

public Image ByteArrayToImage(byte[] buffer)
{
var ms = new MemoryStream(buffer);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

这是我将图像保存到mySql数据库的代码。我使用MySqlWorkbench:

try
{
OpenFileDialog opn = new OpenFileDialog();
opn.Filter = "Choose Image(*.jpg; *.png; *.jpeg)|*.jpg; *.png; *.jpeg";
if (opn.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opn.FileName);
}
}
catch (Exception)
{
throw;
}
MySqlCommand comm;
MySqlDataReader rdr;
//string Query = "UPDATE `chefassist`.`recipy` SET image = '@Img' WHERE name='Herb and Lemon Soup'";
string Query = "UPDATE `chefassist`.`recipy` image=@img where name='" + this.textBox1.Text + "';";
MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid = root; pwd = n0clu387;");
comm = new MySqlCommand(Query, connectToD);
connectToD.Open();
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
MySqlCommand cmd;
//String qry = "UPDATE `chefassist`.`recipy` SET image = '@Img' WHERE name = 'Herb and Lemon Soup'";
string qry = "UPDATE `chefassist`.`recipy` SET image=@img where name='" + this.textBox1.Text + "';";
comm = new MySqlCommand(qry, connectToD);
//comm.Parameters.Add("@Id", MySqlDbType.Int32);
comm.Parameters.Add("@img", MySqlDbType.MediumBlob);
//comm.Parameters["@Id"].Value = txtAddress.Text;
comm.Parameters["@img"].Value = img;
if (comm.ExecuteNonQuery() == 1)
{
MessageBox.Show("Data Inserted");
}
}
catch (Exception)
{
throw;
}

这是我保存ByteArrayToImage:的功能

public Image ByteArrayToImage(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

最后,这是我的显示按钮中的当前代码:

MySqlCommand comm;

//MySqlDataReader rdr;
//MySqlDataAdapter da;
string Query = "select image from chefassist.recipy where image = @img";
using (MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid = root; pwd = n0clu387;"))
{
using (comm = new MySqlCommand(Query, connectToD))
{
comm.Parameters.AddWithValue("@img",textBox1.Text);
connectToD.Close();
try
{
connectToD.Open();
var rdr = comm.ExecuteReader();
while (rdr.Read())
{
byte[] imgg = (byte[])(rdr["image"]);

if (imgg == null)
{
pictureBox2.Image = null;
}
else
{
//pictureBox2.Image = System.Drawing.Image.FromStream(imgg);
pictureBox2.Image = ByteArrayToImage(imgg);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

最新更新