Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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# 如何在c语言中从数据库中检索图像#_C#_Mysql_Visual Studio 2013_Picturebox - Fatal编程技术网

C# 如何在c语言中从数据库中检索图像#

C# 如何在c语言中从数据库中检索图像#,c#,mysql,visual-studio-2013,picturebox,C#,Mysql,Visual Studio 2013,Picturebox,出现一个错误,说明我的代码参数无效。。。 谁能告诉我怎么了?我应该得到分配给客户ID的图像 private void button1_Click(object sender, EventArgs e) { MySqlConnection conn = new MySqlConnection(mycon); MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE client

出现一个错误,说明我的代码参数无效。。。 谁能告诉我怎么了?我应该得到分配给客户ID的图像

private void button1_Click(object sender, EventArgs e)
{
        MySqlConnection conn = new MySqlConnection(mycon);
        MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE clientID='" + label2.Text + "'", conn);

        conn.Open();
        MySqlDataReader myReader = null;
        myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            byte[] imgg = (byte[])(myReader["clientImage"]);
            if (imgg == null)
            {
                pictureBox1.Image = null;
            }
            else
            {
                MemoryStream mstream = new MemoryStream(imgg);
                pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
            }
        }
        conn.Close();
}

这段代码可能会派上用场。我试过了

byte[] imagedata = (byte [])dataGridView1[4, dataGridView1.SelectedRows[0].Index].Value;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imagedata, 0, imagedata.Length))
            {
                ms.Write(imagedata, 0, imagedata.Length);
                //Set image variable value using memory stream.
                image = Image.FromStream(ms, true );
            }

这个代码有效。MySql Wamp

using System.IO;

string appPath = Path.GetDirectoryName(Application.Executable) + @"/student Images/";
string imagename;

//put this code below to load form.
getimage();
if(File.Exist(appPath + imagename)
{
PictureBox1.Image = Image.FromFile(appPath + imagename);
}
else
{
PictureBox1.Image = Properties.Resources.Image_notfound;
}

private void getimage()
{
MySqlConnection connect = new MySqlConnection(con);
MySqlCommand cmd = new MySqlCommand("SELECT PictureName as 'pic' FROM userprofile where ID='" + datagrid.CurrentRow.Cells["ID"].ToString() + "'");
cmd.CommandType = CommandType.Text;
cmd.Connection = connect;
connect.Open();
Try
{
MySqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
imagename = dr.GetString("pic");
}
dr.Close();
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString());
}
finally
{
connect.Close();
}

您在哪一行得到错误?-您不应该将SQL语句连接在一起,而应该使用参数化查询来避免SQL注入