2016-10-09 18 views
1

我正在嘗試更新我現有的某個表中的mu現有圖像字段,並且出現錯誤,提示「GDI +中發生了一般性錯誤」。請幫忙。更新SQL服務器中的圖像字段

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

插入功能是否工作正常,但更新不。我真的很震驚。如果你有更好的方法插入&更新圖片字段請詳細解釋。

+0

爲什麼爲什麼當你顯然知道如何使用SQL參數時,爲什麼要將這個查詢粘合在一起並將所有內容都作爲文本傳遞?你也沒有說你從哪裏得到錯誤 – Plutonix

+0

我在下面的代碼行是「更新記錄」pic_box_employee.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); –

回答

0

首先將你的答案移到你的問題上。它不是一個答案。這只是一個新問題。

嘗試使用以下,我個人更新圖像如下;

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

您的方法與我的一樣..請您強調我的錯誤編碼位置。 –

+0

使用block for memorystream –