简体   繁体   English

更新SQL Server中的Image字段

[英]Updating a Image field in SQL server

I am trying to update mu existing image field in one of my table and I am getting error saying "A generic error occurred in GDI+". 我正在尝试更新我的一个表中的mu现有图像字段,我收到错误说“GDI +中发生了一般错误”。 Please help. 请帮忙。

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

my update code here 我的更新代码在这里

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

Insert function is properly working but the update is not. 插入功能正常工作但更新不正常。 I am really struck in here. 我真的很震惊。 If you have better way to insert & update a image field please explain. 如果您有更好的方法来插入和更新图像字段,请解释。

First of all move your answer to your question. 首先,回答你的问题。 Its not an answer. 这不是答案。 Its just a new question. 这只是一个新问题。

try using the following, I personally update images as follows; 尝试使用以下内容,我个人更新图片如下;

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

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

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