C# 在SQL server中更新图像字段

C# 在SQL server中更新图像字段,c#,C#,我正试图更新我的一个表中的mu现有图像字段,我得到一个错误,说“GDI+中发生了一个通用错误”。请帮忙 private void Add_Records() { int i = check_for_null(); if (i == 1) { DateTime Nw = DateTime.Now; int user = Form_Common_Login.user_id;

我正试图更新我的一个表中的mu现有图像字段,我得到一个错误,说“GDI+中发生了一个通用错误”。请帮忙

private void Add_Records()
    {
        int i = check_for_null();
        if (i == 1)
        {
            DateTime Nw = DateTime.Now;
            int user = Form_Common_Login.user_id;
            int ref_lc = 0;
            ref_lc = Convert.ToInt16(cbo_emp_cat.SelectedValue);

            try
            {
                string query = @"INSERT INTO tbl_Labour_Employee_Reg
                (Ref_IDLC,First_Name,Last_Name,Emp_Image,Designation,NIC_No,Emp_Address,Previous_WorkPlaces,Phone_Number_Mobile,Phone_Number_Residential,Account_Number,Employee_Number,Remarks,Accessed_By,Accessed_Time,Is_Deleted,Is_Active)
                VALUES ('" + ref_lc + "','" + txt_Fname.Text + "','" + txt_Lname.Text + "',@image_array,'" + txt_designation.Text + "','" + txt_nic.Text + "','" + txt_address.Text + "','" + txt_previous_wp.Text + "','" + txt_Mnumber.Text + "','" + txt_Lnumber.Text + "','" + txt_account_number.Text + "','" + txt_emp_number.Text + "','" + txt_remarks.Text + "','" + user + "','" + Nw + "',0,1)";
                clz_Common_SqlConnection con = new clz_Common_SqlConnection();
                SqlCommand cmd = new SqlCommand(query ,con.ActiveCon ());
                MemoryStream ms = new MemoryStream ();
                pic_box_employee.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] image_array = ms.ToArray();
                cmd.Parameters.AddWithValue("@image_array", image_array);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Successfully Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        else { MessageBox.Show("Please enter Valid Data"); }
    }
我的更新代码在这里

private void Update_Records()
    {
        int i = check_for_null();
        if (i == 1)
        {
            DateTime Nw = DateTime.Now;
            int user = Form_Common_Login.user_id;
            int ref_lc = 0;
            ref_lc = Convert.ToInt16(cbo_emp_cat.SelectedValue);

            try
            {
                string update_query = "UPDATE tbl_Labour_Employee_Reg"+
                "SET Ref_IDLC ='"+ref_lc+"',First_Name = ,'" + txt_Fname.Text + "',Last_Name='" + txt_Lname.Text + "',Emp_Image = @image_array,Designation = '" + txt_designation.Text + "',NIC_No='" + txt_nic.Text + "',Emp_Address = '" + txt_address.Text + "'"+
                ",Previous_WorkPlaces = '" + txt_previous_wp.Text + "', Phone_Number_Mobile = '" + txt_Mnumber.Text + "',Phone_Number_Residential='" + txt_Lnumber.Text + "', Account_Number= '" + txt_account_number.Text + "',Employee_Number='" + txt_emp_number.Text + "'"+
                ",Remarks='" + txt_remarks.Text + "',Accessed_By='" + user + "',Accessed_Time= '" + Nw + "',Is_Deleted=0,Is_Active=1";

                clz_Common_SqlConnection con = new clz_Common_SqlConnection();
                SqlCommand cmd = new SqlCommand(update_query, con.ActiveCon());
                MemoryStream ms = new MemoryStream();
                pic_box_employee.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] image_array = ms.ToArray();
                cmd.Parameters.AddWithValue("@image_array", image_array);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Successfully Updated");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                
        }

        else { MessageBox.Show("Please enter Valid Data"); } 
   }

插入函数正常工作,但更新不正常。我在这里真的被打动了。如果您有更好的方法插入和更新图像字段,请解释。

首先移动您的问题答案。这不是答案。这只是一个新问题

尝试使用下面的,我个人更新图片如下

Image img = Image.FromFile(@"C:\Users\Awais\Desktop\image.png");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    arr = ms.ToArray();
}
int dr = 0;
SqlConnection con = new SqlConnection("data source=.; initial catalog=database; uid=sa;pwd=password;");
SqlCommand cmd = new SqlCommand("update table set I1 = @img", con);
cmd.Parameters.AddWithValue("@img", arr);
con.Open();
using (con)
{
    dr = cmd.ExecuteNonQuery();
}

首先,移动你的问题的答案。这不是答案。这只是一个新问题

尝试使用下面的,我个人更新图片如下

Image img = Image.FromFile(@"C:\Users\Awais\Desktop\image.png");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    arr = ms.ToArray();
}
int dr = 0;
SqlConnection con = new SqlConnection("data source=.; initial catalog=database; uid=sa;pwd=password;");
SqlCommand cmd = new SqlCommand("update table set I1 = @img", con);
cmd.Parameters.AddWithValue("@img", arr);
con.Open();
using (con)
{
    dr = cmd.ExecuteNonQuery();
}

当您显然知道如何使用SQL参数时,为什么要将该查询粘合在一起并将所有内容作为文本传递?你也没有说你从哪里得到错误我在“更新记录”pic_box_employee.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg)下面的代码行中得到错误;当您显然知道如何使用SQL参数时,为什么要将该查询粘合在一起并将所有内容作为文本传递?你也没有说你从哪里得到错误我在“更新记录”pic_box_employee.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg)下面的代码行中得到错误;你的方法和我的一样。你能不能强调一下我在哪里做了错误的编码。你的方法和我的一样。你能不能强调一下我在哪里做了错误的编码