','근처에 구문이 잘못되었습니다. 업데이트 쿼리를 사용하는 중

2013-03-21 2 views
0

다음은 내 코드입니다. 내 웹 사이트에서 클래스를 사용했습니다. 그러나 '잘못된 구문이 근처에 있습니다', '...'라는 오류 메시지가 나타납니다.','근처에 구문이 잘못되었습니다. 업데이트 쿼리를 사용하는 중

protected void btnadd_Click(object sender, EventArgs e) 
    { 
     s1 = "Update tckt_tbl Set class='" + ddlclass.Text + "',dist=" + lbldist.Text + ",no_of_adults=" + ddladults.Text + ",no_of_senior=" + ddlsenior.Text + ",n1='" + txtn1.Text + "',"; 
     s1 += "n2='" + txtn2.Text + "',n3='" + txtn3.Text + "',n4='" + txtn4.Text + "',n5='" + txtn5.Text + "',n6='" + txtn6.Text + "',"; 
     s1 += "ag1=" + txtag1.Text + ",ag2=" + txtag2.Text + ",ag3=" + txtag3.Text + ",ag4=" + txtag4.Text + ",ag5=" + txtag5.Text + ",ag6=" + txtag6.Text + ","; 
     s1 += "gen1='" + txtgen1.Text + "',gen2='" + txtgen2.Text + "',gen3='" + txtgen3.Text + "',gen4='" + txtgen4.Text + "',gen5='" + txtgen5.Text + "',gen6='" + txtgen6.Text + "',"; 
     s1 += "cn1='" + txtchn1.Text + "',cn2='" + txtchn2.Text + "',cag1=" + txtcag1.Text + ",cag2=" + txtcag2.Text + ",cgen1='" + txtcgen1.Text + "',cgen2='" + txtcgen2.Text + "' Where userid=" + Session["suser"].ToString() + ""; 
     con.ExecQuery(s1); 
     Response.Redirect("tcktbook_itckt.aspx"); 
    } 
+5

[SqlParameter] (http://msdn.microsoft.com/en-us/library)를 사용하십시오. /system.data.sqlclient.sqlparameter.aspx)이 옵션을 선택하면 [SQL Injection] (http://en.wikipedia.org/wiki/SQL_injection)에서 저장되지만 ** 쿼리가 더 읽기 쉽습니다 **. – Habib

+0

디버거를 사용하고 s1에 어떤 값이 오는지 확인하십시오. –

답변

0

첫째로, 그것은 query from user input을 구축하는 really bad idea입니다 .. 코드에서 잘못된 것입니다. 대신 parameterized query을 사용하는 것이 좋습니다.

s1을 출력하여 쿼리의 모양을 확인하십시오.

불균형 한 따옴표가있는 것이 좋습니다.

0

세 번째 줄에는 문제가 있습니다.

s1 += "ag1=" + txtag1.Text + ",ag2=" + txtag2.Text + ",ag3=" + txtag3.Text + ",ag4=" + txtag4.Text + ",ag5=" + txtag5.Text + ",ag6=" + txtag6.Text + ","; 

여기서는 쉼표 및 세미콜 런이 제대로 주어지지 않았다고 생각합니다.

0

모든 것을 연결하는 대신 매개 변수 쿼리를 사용하는 것이 좋습니다. 하지만 그 전에는 이미 모든 가치를 제공하고 있습니까? 숫자 필드에 값을 지정하지 않으면 쿼리가 다음과 같이 나타나므로 오류가 표시됩니다.

..cag1=,cag2.. 
관련 문제