2017-05-02 1 views
0

내 데이터베이스에 저장된 사용자 데이터를 업데이트 할 수 없습니다.C에서 업데이트 sql 명령 #

내가하는 버튼을 생성 한 다음 C# 코드 여기

를 추가 한 내 영문 코드 : 내가 업데이트 명령 "사용자 업데이트"를 시도하고 다음 "이름을 설정"

table class="logintable" border="0" cellpadding="0" cellspacing="0"> 
     <tr> 

     </tr> 
     <tr> 
      <td> 
       Fornavn 
      </td> 
      <td> 
       <asp:TextBox ID="txtFornavn" runat="server" /> 
      </td> 

      <td> 
       <asp:Button ID="Button1" Text="Update" runat="server" OnClick="Button1_Click" /> 
      </td> 
      <td> 
      </td> 
     </tr> 
    </table> 

여기 당신이 찾고있는 무슨이 아마 내 C# 코드

protected void Button1_Click(object sender, EventArgs e) 
     { 

      using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=testingDB;User id=sqluser2;Password=password01;")) 
      { 

       SqlCommand command1 = new SqlCommand("UPDATE Bruger SET Fornavn='" + txtFornavn.Text + "' where Email='"+ Session["Email"] + "';",con); 

       //SqlCommand command1 = new SqlCommand(); 
       //command1 = con.CreateCommand(); 

       //command1.CommandText = "UPDATE Bruger set fornavn='" + txtFornavn.Text+ "' where email='" + Session["email"] + "';"; 

       command1.Parameters.AddWithValue("@Fornavn", txtFornavn.Text); 



       command1.Connection.Open(); 
       command1.ExecuteNonQuery(); 



       con.Close(); 
+0

무엇이 묻습니까? – TheGameiswar

+0

txtFornavn.Text에 의한 SQL 주입 경고, 거기에 매개 변수가있는 것처럼 보입니다. – SCFi

+0

전자 메일이''OR 1 = 1 -'인 경우 어떻게합니까? –

답변

0

입니다 :

protected void Button1_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=testingDB;User id=sqluser2;Password=password01;")) 
    { 
     SqlCommand command1 = new SqlCommand("UPDATE Bruger SET [email protected] where [email protected];",con); 
     command1.Parameters.AddWithValue("@Fornavn", txtFornavn.Text); 
     command1.Parameters.AddWithValue("@email", Session["Email"]); 

     command1.Connection.Open(); 
     command1.ExecuteNonQuery(); 
    } 
} 
+0

너무 빠르게 입력하십시오. 단순한 초 단위로 나를 두드리는 것 –

+1

아마도'using (SqlCommand command1 = new SqlCommand (...' –

0

SqlCommand는 절대로 인라인 변수로 구성하면 안됩니다. 이것은 SQL Injection으로 연결됩니다.

받은 예외가 없으므로 매개 변수가없는 명령에 매개 변수를 추가하여 내가 본 결함을 지적해야합니다.

내 교체 코드는 원래 명령에서 인라인 변수를 제거하고 매개 변수 할당을 유지하고 전자 메일 주소로 초를 추가합니다.

SqlCommand command1 = new SqlCommand("UPDATE Bruger SET [email protected] where [email protected]",con); 

command1.Parameters.AddWithValue("@Fornavn", txtFornavn.Text); 
command1.Parameters.AddWithValue("@FEmail", (string)Session["email"]); 

command1.Connection.Open(); 
command1.ExecuteNonQuery();