2013-11-24 3 views
1
using System.Data.SqlClient; 
using System.Data.Sql; 

public partial class _Default : System.Web.UI.Page 
{ 


SqlConnection con = new SqlConnection(@"Data Source=GAGAN-PC\SQLEXPRESS;Initial Catalog=update_test;Integrated Security=True"); 
    SqlCommand cmd; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void delete_button_Click(object sender, EventArgs e) 
    { 
     con.Open(); 
     cmd = new SqlCommand("delete from update_delete where id like'"+TextBox1.Text+"'",con); 
     cmd.ExecuteNonQuery(); 
     Response.Write("Control reached."); 
     con.Close(); 
     Response.Write("Data successfully deleted."); 
    } 
    protected void update_button_Click(object sender, EventArgs e) 
    { 
     con.Open(); 
     cmd = new SqlCommand("update update_delete set password ='"+TextBox3.Text+"' where id like'"+TextBox2+"'", con); 
     cmd.ExecuteNonQuery(); 
     Response.Write("Control reached."); 
     con.Close(); 
     Response.Write("Data successfully Updated."); 
    } 
} 

ASP.NET 웹 응용 프로그램에서 작동하지 않습니다. SQL Server를 데이터베이스로 사용하고 update_delete은 3 열 id,sname,password이 있고 id와 관련하여 암호를 업데이트하려고하는 테이블입니다.업데이트 쿼리 내가 업데이트 쿼리를 구현하기 위해 노력하고 있지만, 약간의 문제가 거기에있다

문제는 내가 업데이트 버튼 컨트롤을 클릭하면 cmd.ExecuteNonQuery();에 도달하면 오류가 표시됩니다. 업데이트는 일어나지 않습니다. 내가 어떻게해야하지? 제발 제발 나를 도와주세요. 미리 감사드립니다. :) :)

+1

이렇게 많은'IDisposable' 객체. ( –

+1

) 많은 쿼리, 너무 적은 매개 변수 - 너무 많은 ** SQL Injection ** 위험! –

+0

잘못된, marc_s INT와 같은 숫자 데이터 유형에 LIKE OPERATOR를 사용할 수 있습니다. 하지만 예상대로 작동합니다. – laylarenee

답변

1

나는 당신이 예외를 얻는다고 생각한다. 예외를 잡아 내고 메시지를 알려주는 것이 좋습니다 ... 디버거 또는 try-catch 절을 사용하여 예외를 catch 할 수 있습니다.

예외가 발생하지 않고 "제어 도달"메시지가 표시되면 구성된 SQL 문자열을 사용하여 SQL Server에서 직접 사용하고 SQL 문에 실수가 있는지 확인해야합니다. 예를 들어, 존재하지 않는 ID를 사용하여 잘못된 SQL 문을 작성했다고 가정합니다.

희망이 있습니다.

2

저는 여기서 추측하고 있습니다. Id이 숫자 데이터 유형 인 경우 LIKE을 사용할 수 없습니다.

또한 : 적절한 처리를 보장하고 SQL 주입 공격을 피하기 위해 매개 변수화 쿼리를 사용하는 using()... 블록을 사용하십시오.

이처럼 UPDATE 명령을 쓰기 :

protected void update_button_Click(object sender, EventArgs e) 
{ 
    // get the values to use 
    string idValue = Convert.ToInt32(TextBox3.Text.Trim()); 
    string password = TextBox2.Text.Trim(); 

    // define the query text with *parameters* ! 
    string updateQuery = "update update_delete set password = @password where id = @ID"; 

    // put things like SqlConnection and SqlCommand into "using()...." blocks 
    using (SqlCommand updCmd = new SqlCommand(updateQuery, con)) 
    { 
     // define parameters and their values 
     updCmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = password; 
     updCmd.Parameters.Add("@ID", SqlDbType.Int).Value = idValue; 

     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 

     Response.Write("Data successfully Updated."); 
    } 
} 
+1

또 다른 중요한 점은 암호를 일반 텍스트로 저장해서는 안된다는 것입니다. – laylarenee

+0

'// 매개 변수와 그 값을 정의하십시오 '형식을 지정하는 요점이 있습니까? (예 : TVP) 'AddWithValue'는 항상 저에게 잘 돌아 갔고 코드는 훨씬 적습니다. –

+0

@ ta.speot.is : 개인적인 취향입니다. 내 유형이 무엇인지 ** 알기 ** ADO.NET을 참조하십시오. 'NULL'을 전달하면 어떻게 될까요? ?? –

관련 문제