从Sql中检索图像,“参数无效”

时间:2019-03-05 11:39:57

标签: c# sql-server winforms

我无法从SQL Server检索图像。

  

“参数无效”

获取图像代码

SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("select [image] from employee where empcode = 0067", con);
                SqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    byte[] img = (byte[])(reader["image"]);
                    if (img == null)
                        pictureBox1.Image = null;
                    else
                    {
                        MemoryStream ms = new MemoryStream(img);
                        pictureBox1.Image = Image.FromStream(ms); 

                    }
                }

选择图像代码

OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Images(.jpg,.png)|*.png;*.jpg";
            ofd.Title = "Select Employee Picture";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                imagefilePath = ofd.FileName.ToString();
                pictureBox1.Image = Image.FromFile(ofd.FileName);

            }

插入图片代码

MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] a = ms.GetBuffer();
            ms.Close();

    String query = "INSERT INTO employee ([empcode],[fullname],[fathername],[cnic],[empAddress],[phone],[dateofBirth],[department],[designation],[salary],[dateofJoining],[status],[workingTime],[image]) values (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@img)";
                cmd = new SqlCommand(query, con);

                cmd.Parameters.Add(new SqlParameter("@d1", SqlDbType.VarChar, 30, "empcode"));
                cmd.Parameters.Add(new SqlParameter("@d2", SqlDbType.VarChar, 30, "fullname"));
                cmd.Parameters.Add(new SqlParameter("@d3", SqlDbType.VarChar, 30, "fathername"));
                cmd.Parameters.Add(new SqlParameter("@d4", SqlDbType.VarChar, 30, "cnic"));
                cmd.Parameters.Add(new SqlParameter("@d5", SqlDbType.VarChar, 30, "empAddress"));
                cmd.Parameters.Add(new SqlParameter("@d6", SqlDbType.VarChar, 30, "phone"));
                cmd.Parameters.Add(new SqlParameter("@d7", SqlDbType.VarChar, 30, "dateofBirth"));
                cmd.Parameters.Add(new SqlParameter("@d8", SqlDbType.Int, 30, "department"));
                cmd.Parameters.Add(new SqlParameter("@d9", SqlDbType.Int, 30, "designation"));
                cmd.Parameters.Add(new SqlParameter("@d10", SqlDbType.Int, 30, "salary"));
                cmd.Parameters.Add(new SqlParameter("@d11", SqlDbType.VarChar, 30, "dateofJoining"));
                cmd.Parameters.Add(new SqlParameter("@d12", SqlDbType.VarChar, 30, "status"));
                cmd.Parameters.Add(new SqlParameter("@d13", SqlDbType.VarChar, 30, "workingTime"));


                cmd.Parameters["@d1"].Value = edtEmpCode.Text;
                cmd.Parameters["@d2"].Value = edtFullname.Text;
                cmd.Parameters["@d3"].Value = edtFatherName.Text;
                cmd.Parameters["@d4"].Value = edtCnic.Text;
                cmd.Parameters["@d5"].Value = edtAddress.Text;
                cmd.Parameters["@d6"].Value = edtPhone.Text;
                cmd.Parameters["@d7"].Value = dateofBirth.Text;
                cmd.Parameters["@d8"].Value = dropdownDepartment.SelectedValue;
                cmd.Parameters["@d9"].Value = dropdownDesignation.SelectedValue;
                cmd.Parameters["@d10"].Value = edtSalary.Text;
                cmd.Parameters["@d11"].Value = appointmentDate.Text;
                cmd.Parameters["@d12"].Value = "Active";
                cmd.Parameters["@d13"].Value = workingTime.Text;
                cmd.Parameters.AddWithValue("@img",a);


                cmd.ExecuteNonQuery();

请提供一些有效的解决方案。

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您在将其插入数据库时​​需要传递如下的image参数,并确保表中的image列数据类型为image。

cmd.Parameters.Add(new SqlParameter("@img", SqlDbType.Image,0, "column name"));