Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#winform应用程序中将图像从MySql加载到PictureBox2时,参数无效_C#_Mysql - Fatal编程技术网

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

在C#winform应用程序中将图像从MySql加载到PictureBox2时,参数无效,c#,mysql,C#,Mysql,请帮我解决这个问题,因为我已经尝试了许多来自多个来源的解决方案。 我有一个带显示按钮的pictureBox2。这个想法是,如果我单击show按钮,JPEG图像将出现在myMySqlDB的pictureBox2中。该图像位于名为Recipy的表中的blob字段中(请原谅我的配方拼写错误)。这是我的代码: MySqlCommand comm; MySqlDataReader rdr; MySqlDataAdapter da; string Query = "select image fr

请帮我解决这个问题,因为我已经尝试了许多来自多个来源的解决方案。 我有一个带显示按钮的pictureBox2。这个想法是,如果我单击show按钮,JPEG图像将出现在my
MySql
DB的pictureBox2中。该图像位于名为Recipy的表中的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);
  }
您可以尝试使用此方法转换字节数组图像

 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);
                    }
                }
            }
        }

您的查询易受SQL注入攻击。考虑使用谢谢你的帮助。我得到这个错误信息,说不能将字节[]转换成Soal.Io.St流。有什么想法吗?哪一行会出错?这个:var ms=newmemoryStream(buffer)?pictureBox2.Image=System.Drawing.Image.FromStream(imgg);对,对不起,我改了密码。事实上,您应该用pictureBox2.Image=ByteArrayToImage(缓冲区)替换它;我为我的痛苦道歉,但我仍然让护理人员无效