简体   繁体   English

在图片框中设置图片从SQL数据库中检索

[英]Setting image in picture box retrieving from sql database

This is my code to retrieve a image from sql database in C#. 这是我的代码,用于从C#中的sql数据库检索图像。 i want to set the image into a picturebox but im getting "system.drawing.dll parameter is not valid" 我想将图像设置为图片框,但即时通讯收到“ system.drawing.dll参数无效”

byte[] getImg;
     SqlConnection con2 = new SqlConnection(conString);
                con2.Open();
                if (con2.State == System.Data.ConnectionState.Open)
                {
                    string sss = "SELECT student_photo from student_reg where reg_year='" + year + "'and s_id='" +sid_lbl.Text+ "'";
                    SqlCommand cmd = new SqlCommand(sss, con2);
                    Console.WriteLine(sss);
                    SqlDataReader dr4 = cmd.ExecuteReader();

                    while (dr4.Read())
                    {



                        getImg = (byte[])(dr4["student_photo"]);






                    }
                    MemoryStream stream = new MemoryStream(getImg);
                    picturebox1.Image = Image.FromStream(stream);
    }
    con2.Close()

It's hard to pinpoint where your issue might be, but try a couple of changes: 很难确定您的问题可能出在哪里,但是请尝试一些更改:

  1. Use parameters instead of hard-coding. 使用参数而不是硬编码。 In your example, the single quotes implied that id and reg_year were both strings. 在您的示例中,单引号表示idreg_year都是字符串。 If this isn't the case, you want to strong-datatype your parameter values (convert to Int32 or whatever matches your database). 如果不是这种情况,则需要对参数值进行强数据类型化(转换为Int32或与数据库匹配的任何值)。 Can you attach the DDL for the table? 您可以为表格附加DDL吗?

  2. If your output is one row / one column, consider ExecuteScalar . 如果输出是一行/一列,请考虑ExecuteScalar

If this doesn't work, my guess is that's not really an image file in the student_photo field. 如果这不起作用,我猜想那不是student_photo字段中的图像文件。

Code: 码:

SqlConnection con2 = new SqlConnection(conString);
con2.Open();

if (con2.State == System.Data.ConnectionState.Open)
{
    string sss = "SELECT student_photo from student_reg where reg_year=@YEAR and s_id=@ID";
    SqlCommand cmd = new SqlCommand(sss, con2);
    cmd.Parameters.AddWithValue("@YEAR", year);
    cmd.Parameters.AddWithValue("@ID", sid_lbl.Text);

    Byte[] getImg = (Byte[])cmd.ExecuteScalar();
    MemoryStream stream = new MemoryStream(getImg);
    Image img = Image.FromStream(stream);

    picturebox1.Image = img;
}
con2.Close()

Check the following Code: 检查以下代码:

string stuImg  = string.Empty;       
         SqlConnection con2 = new SqlConnection(conString);
            con2.Open();
            if (con2.State == System.Data.ConnectionState.Open)
            {
                string sss = "SELECT student_photo from student_reg where  reg_year='" + year + "'and s_id='" +sid_lbl.Text+ "'";
                SqlCommand cmd = new SqlCommand(sss, con2);
                Console.WriteLine(sss);
                SqlDataReader dr4 = cmd.ExecuteReader();
                while (dr4.Read())
                {
                    stuImg =dr4["student_photo"].ToString();
                }
                var pic = Convert.FromBase64String(stuImg);
                using (MemoryStream ms = new MemoryStream(pic))
                  {
                   picturebox1.Image = Image.FromStream(ms);
                  }
              }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM