2015-07-10 47 views
0

我使用Visual Studio 2013和SQL Server 2014不正確的語法我得到一個錯誤的SQL Server 2014:不遠的地方

附近有語法錯誤在哪裏AD ='

我是初學者所以我無法弄清楚問題並需要你的幫助。

這裏是我的代碼:

private void btngno_Click(object sender, EventArgs e) 
{ 
    SqlConnection baglan = new SqlConnection("Server=.;Database=lalala;Trusted_Connection=true;"); 
    baglan.Open(); 

    SqlCommand cmd2 = new SqlCommand("UPDATE ilktablom SET gno= " + Int32.Parse(gnotxt.Text) + "'Where Ad= '" + txtAd.Text + "' ,Soyad= '" + txtSoyad.Text + "' ,Sifre= '" + txtSifre.Text, baglan); 
    if (cmd2.ExecuteNonQuery() == 1) 
    { 
     MessageBox.Show("Process completed."); 
    } 
    else 
    { 
     MessageBox.Show("Process not completed."); 
    } 
}  

回答

4

你的SQL,你生成(除了是開放的SQL注入)缺少終止',並WHERE條款(而不是AND)在使用逗號

相反,你可以這樣做:

private void btngno_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection baglan = new SqlConnection("Server=.;Database=lalala;Trusted_Connection=true;")) 
    { 
     baglan.Open(); 

     using (SqlCommand cmd2 = new SqlCommand("UPDATE ilktablom SET gno = @gno Where Ad = @Ad AND Soyad= @Soyad AND Sifre = @Sifre", baglan)) 
     { 
      cmd2.Parameters.Add("@gno", SqlDbType.Int).Value = gnotxt.Text; 
      cmd2.Parameters.Add("@Ad", SqlDbType.Varchar).Value = txtAd.Text; 
      cmd2.Parameters.Add("@Soyad", SqlDbType.Varchar).Value = txtSoyad.Text; 
      cmd2.Parameters.Add("@Sifre", SqlDbType.Varchar).Value = txtSifre.Text; 
      if (cmd2.ExecuteNonQuery() == 1) 
      { 
       MessageBox.Show("Process completed."); 
      } 
      else 
      { 
       MessageBox.Show("Process not completed."); 
      } 
     } 
    } 
} 
1

錯誤文本是不言自明的。

你真的有不正確的語法在這裏:

Where Ad= '" + txtAd.Text + "' ,Soyad= '..... 

這concatenction產生查詢像

Where Ad='something', Soyad = 'something'..., 

但是在SQL服務器情況應使用andor和其他邏輯運算符,而不是逗號加盟。

所以它應該是類似的(可能不是and,但應該使用or運算符 - 從上下文中不清楚)。

Where Ad='something' and Soyad = 'something'..., 

另請注意,連接您的查詢文本使您無法抵禦SQL注入。請考慮改用parameterized查詢。