2017-02-23 3 views
0

소프트웨어에서 데이터를 업데이트 할 때 교착 상태 오류가 발생합니다. marc_s 이미 언급 한 바와 같이, 사용자가 직접 입력에서 쿼리을 연결하는 것은 위험하다, 모든트랜잭션 프로세스 - 교착 상태 오류

private void btn_upd_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SqlConnection con = new SqlConnection(Constr); 
     con.Open(); 
     string myquery = "Select Reg,Rank,Trade,Name,Wing,Father_name,Dob,Gender,Cast,Sect,Serial,Qualification,Tehseel,District,Cnic_No,Blood_Group,Height,Weight,Identification_Mark,Permanent_Add,Nameof_Spouse,Relation,Nameof_MaleKids,image1 from PersonalInfo where Reg='" + txt_srch.Text + "'"; 

     SqlCommand c = new SqlCommand(myquery, con); 

     SqlDataReader rd = c.ExecuteReader(); 

     if (!(rd.HasRows)) 
     { 
      MessageBox.Show("No such data to delete"); 
     } 
     else 
     { 
      string query; 

      query = "update PersonalInfo set Rank='" + textBox2.Text + "', Serial='" + SN.Text + "', Trade='" + textBox3.Text + "',Name='" + textBox4.Text + "',Wing='" + textBox5.Text + "',Father_name='" + textBox6.Text + "',Dob='" + textBox7.Text + "',Gender='" + textBox8.Text + "',Cast='" + textBox9.Text + "',Sect='" + textBox23.Text + "',Qualification='" + textBox10.Text + "',Tehseel='" + textBox24.Text + "',District='" + textBox21.Text + "',Cnic_No='" + textBox11.Text + "',Blood_Group='" + textBox12.Text + "',Height='" + textBox13.Text + "',Weight='" + textBox14.Text + "',Identification_Mark='" + textBox15.Text + "',Permanent_Add='" + textBox16.Text + "',Nameof_Spouse='" + textBox18.Text + "',Relation='" + textBox19.Text + "',Nameof_MaleKids='" + textBox20.Text + "',image1='" + ImageToBase64(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Jpeg) + "' where Reg='" + txt_srch.Text + "'"; 

      SqlCommand cmd = new SqlCommand(query, con); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Data Updated"); 
     } 
     con.Close(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+1

[SQL 인젝션 경고] (http://msdn.microsoft.com/en-us/library/ms161953%28v= :

둘째, 자신의 연결 Open()Close() 내에서 각 쿼리를 실행 해보십시오 sql.105 % 29.aspx) - SQL 문을 연결하지 말고 SQL 주입을 피하기 위해 ** 매개 변수화 된 쿼리 **를 사용해야합니다. [Little Bobby Tables] (https://xkcd.com)를 확인하십시오./327 /) –

답변

0

첫째 :

여기 내 코드입니다. 제안 된대로 해당 메소드를 변경하고 paramterized 쿼리를 사용해야합니다.

private void btn_upd_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SqlConnection con = new SqlConnection(Constr); 

     con.Open(); 

     string myquery = "Select Reg,Rank,Trade,Name,Wing,Father_name,Dob,Gender,Cast,Sect,Serial,Qualification,Tehseel,District,Cnic_No,Blood_Group,Height,Weight,Identification_Mark,Permanent_Add,Nameof_Spouse,Relation,Nameof_MaleKids,image1 from PersonalInfo where Reg='" + txt_srch.Text + "'"; 

     SqlCommand c = new SqlCommand(myquery, con); 

     SqlDataReader rd = c.ExecuteReader(); 

     con.Close(); 

     if (!(rd.HasRows)) 
     { 
      MessageBox.Show("No such data to delete"); 
     } 
     else 
     { 
      con.Open(); 

      string query; 

      query = "update PersonalInfo set Rank='" + textBox2.Text + "', Serial='" + SN.Text + "', Trade='" + textBox3.Text + "',Name='" + textBox4.Text + "',Wing='" + textBox5.Text + "',Father_name='" + textBox6.Text + "',Dob='" + textBox7.Text + "',Gender='" + textBox8.Text + "',Cast='" + textBox9.Text + "',Sect='" + textBox23.Text + "',Qualification='" + textBox10.Text + "',Tehseel='" + textBox24.Text + "',District='" + textBox21.Text + "',Cnic_No='" + textBox11.Text + "',Blood_Group='" + textBox12.Text + "',Height='" + textBox13.Text + "',Weight='" + textBox14.Text + "',Identification_Mark='" + textBox15.Text + "',Permanent_Add='" + textBox16.Text + "',Nameof_Spouse='" + textBox18.Text + "',Relation='" + textBox19.Text + "',Nameof_MaleKids='" + textBox20.Text + "',image1='" + ImageToBase64(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Jpeg) + "' where Reg='" + txt_srch.Text + "'"; 

      SqlCommand cmd = new SqlCommand(query, con); 

      cmd.ExecuteNonQuery(); 

      con.Close(); 

      MessageBox.Show("Data Updated"); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

네 이것이 맞는 것입니다. 나를 때려! @Syed 트랜잭션, 저장 프로 시저 또는 함수의 일부로 SQL을 결합하지 않는 한 ONE cmd 객체 내부에서 여러 SQL 명령을 수행 할 수 없습니다. – Fandango68

관련 문제