2017-04-08 29 views
1

我不明白問題所在,我嘗試修復,搜索並找不到問題。C#/ SQL語法錯誤

','附近的語法不正確。

enter image description here

代碼:

using System.Data.SqlClient; 

SqlConnection ABC = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\kindl\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\bd_formular.mdf;Integrated Security=True;Connect Timeout=30"); 

SqlCommand comm = new SqlCommand(); 

SqlDataReader dataRead; 

private void B1_Click(object sender, EventArgs e) 
{ 
    ABC.Open(); 
    comm.CommandText = "insert into dbo.bd_formular(facultate,domeniul,specializare,forma_invatamant,d_inscriere,nume_prenume,cod,localitate,judet,tara,strada,numar,bloc,scara,etaj,apartament,sector,cod_p,data_nasterii,locul_nasterii,judet_n,tara_n,sex,starea_civila,cetatenie,cetatenie_op,etnie,cnp,serie,numar_cnp,eliberat,e_data,studii_preuni,nume_unitate,spec_fili_profil,oras_s,,judet_s,tara_s,forma_de_invatamant,medie_bac,durata_studii,data_absolvirii,tipul_diploma,seria_diploma,numarul_diploma,emis_de_catre,data_emiterii,nr_foi_matricole,introducere_date,semnatura)values('" 
    + C1.Text + "','" + T1.Text + "','" + T2.Text + "','" + C2.Text + "','" + DTP1.Value.ToString("MM/dd/yyyy") + "','" + T3.Text + "','" + T4.Text + "','" + T5.Text + "','" + T6.Text + "','" + T7.Text + "','" 
    + T8.Text + "','" + T9.Text + "','" + T10.Text + "','" + T11.Text + "','" + T12.Text + "','" + T13.Text + "','" + T14.Text + "','" + T15.Text + "','" + DTP2.Value.ToString("MM/dd/yyyy") + "','" + T16.Text + "','" + T17.Text + "','" + T18.Text + "','" + C3.Text + "','" + C4.Text + "','" + C5.Text + "','" + T19.Text + "','" + T20.Text + "','" + C6.Text + "','" + T21.Text + "','" + T22.Text + "','" + T23.Text + "','" + T24.Text + "','" + DTP3.Value.ToString("MM/dd/yyyy") + "','" + C7.Text + "','" + T25.Text 
    + "','" + T26.Text + "','" + T27.Text + "','" + T28.Text + "','" + T29.Text + "','" + C8.Text + "','" + T30.Text + "','" + C9.Text + "','" + DTP4.Value.ToString("MM/dd/yyyy") + "','" + C10.Text + "','" + T31.Text + "','" + T32.Text + "','" + T33.Text + "','" + DTP5.Value.ToString("MM/dd/yyyy") + "','" + T34.Text + "','" + T35.Text + "','" + T36.Text + "')"; 

    comm.ExecuteNonQuery(); 
    ABC.Close(); 

    MessageBox.Show("Adaugat cu succes!"); 
} 
+1

瞭解如何使用參數來替代查詢字符串。這將防止意外的語法錯誤。 –

+1

使用Sql參數,請參見[最佳實踐 - 執行Sql語句](http://stackoverflow.com/documentation/.net/3589/ado-net/14261/best-practices-executing-sql-statements)。同樣使用'using'語句,不要共享連接實例(再次參見鏈接)。最後瞭解什麼是例外以及如何閱讀它們。 – Igor

回答

2

正如已經指出的那樣,異常是由您的聲明中的雙重,造成的。真的,你需要使用Sql參數。也使用using聲明,並且不共享連接實例(請參閱鏈接)。

// store this in the app.config instead of hard coding 
const string SqlConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\kindl\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\bd_formular.mdf;Integrated Security=True;Connect Timeout=30"; 

private void B1_Click(object sender, EventArgs e) 
{ 
    const string sqlText = "INSERT INTO dbo.bd_formular(facultate,domeniul,specializare,forma_invatamant,d_inscriere,nume_prenume,cod,localitate,judet,tara,strada,numar,bloc,scara,etaj,apartament,sector,cod_p,data_nasterii,locul_nasterii,judet_n,tara_n,sex,starea_civila,cetatenie,cetatenie_op,etnie,cnp,serie,numar_cnp,eliberat,e_data,studii_preuni,nume_unitate,spec_fili_profil,oras_s,judet_s,tara_s,forma_de_invatamant,medie_bac,durata_studii,data_absolvirii,tipul_diploma,seria_diploma,numarul_diploma,emis_de_catre,data_emiterii,nr_foi_matricole,introducere_date,semnatura) VALUES (@facultate,@domeniul,@specializare,@forma_invatamant,@d_inscriere,@nume_prenume,@cod,@localitate,@judet,@tara,@strada,@numar,@bloc,@scara,@etaj,@apartament,@sector,@cod_p,@data_nasterii,@locul_nasterii,@judet_n,@tara_n,@sex,@starea_civila,@cetatenie,@cetatenie_op,@etnie,@cnp,@serie,@numar_cnp,@eliberat,@e_data,@studii_preuni,@nume_unitate,@spec_fili_profil,@oras_s,@judet_s,@tara_s,@forma_de_invatamant,@medie_bac,@durata_studii,@data_absolvirii,@tipul_diploma,@seria_diploma,@numarul_diploma,@emis_de_catre,@data_emiterii,@nr_foi_matricole,@introducere_date,@semnatura)"; 
    // use using statements to ensure connections are closed and resources are freed 
    using(var con = new SqlConnection(SqlConnectionString)) 
    using(var comm = new SqlCommand(sqlText, con)) 
    { 
     comm.Parameters.Add(new SqlParameter("@facultate", SqlDbType.VarChar){Value = C1.Text}); 
     comm.Parameters.Add(new SqlParameter("@domeniul", SqlDbType.VarChar){Value = T1.Text}); 
     // etc, fill this in with the remaining parameters 

     con.Open(); 
     comm.ExecuteNonQuery(); 
     // not really a great place for this, I recommend splitting the ADO.NET code from the UI code 
     MessageBox.Show("Adaugat cu succes!"); 
    } 
} 

最後瞭解什麼是異常以及如何閱讀它們。

+0

我確實做了你寫的,但現在,我需要聲明「@」(必須聲明標量變量「@」) –

+0

@IulianGlăvan - 我想你已經弄明白了,但萬一忘了刪除double ',,'從原來的查詢中查找,這也受find/replace的影響,用參數名稱創建相應的字符串。我用修復程序更新了答案。 – Igor

3

你有兩個逗號先後。刪除一樣。

enter image description here

建議:請使用參數化查詢。

-1

這是正確的查詢嘗試

using System.Data.SqlClient; 
SqlConnection ABC = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\kindl\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\bd_formular.mdf;Integrated Security=True;Connect Timeout=30"); 
      SqlCommand comm = new SqlCommand(); 
      SqlDataReader dataRead; 

      private void B1_Click(object sender, EventArgs e) 
      { 





        ABC.Open(); 
        comm.CommandText = "insert into dbo.bd_formular(facultate,domeniul,specializare,forma_invatamant,d_inscriere,nume_prenume,cod,localitate,judet,tara,strada,numar,bloc,scara,etaj,apartament,sector,cod_p,data_nasterii,locul_nasterii,judet_n,tara_n,sex,starea_civila,cetatenie,cetatenie_op,etnie,cnp,serie,numar_cnp,eliberat,e_data,studii_preuni,nume_unitate,spec_fili_profil,oras_s,judet_s,tara_s,forma_de_invatamant,medie_bac,durata_studii,data_absolvirii,tipul_diploma,seria_diploma,numarul_diploma,emis_de_catre,data_emiterii,nr_foi_matricole,introducere_date,semnatura)values('" 
    + C1.Text + "','" + T1.Text + "','" + T2.Text + "','" + C2.Text + "','" + DTP1.Value.ToString("MM/dd/yyyy") + "','" + T3.Text + "','" + T4.Text + "','" + T5.Text + "','" + T6.Text + "','" + T7.Text + "','" 
    + T8.Text + "','" + T9.Text + "','" + T10.Text + "','" + T11.Text + "','" + T12.Text + "','" + T13.Text + "','" + T14.Text + "','" + T15.Text + "','" + DTP2.Value.ToString("MM/dd/yyyy") + "','" + T16.Text + "','" + T17.Text + "','" + T18.Text + "','" + C3.Text + "','" + C4.Text + "','" + C5.Text + "','" + T19.Text + "','" + T20.Text + "','" + C6.Text + "','" + T21.Text + "','" + T22.Text + "','" + T23.Text + "','" + T24.Text + "','" + DTP3.Value.ToString("MM/dd/yyyy") + "','" + C7.Text + "','" + T25.Text 
    + "','" + T26.Text + "','" + T27.Text + "','" + T28.Text + "','" + T29.Text + "','" + C8.Text + "','" + T30.Text + "','" + C9.Text + "','" + DTP4.Value.ToString("MM/dd/yyyy") + "','" + C10.Text + "','" + T31.Text + "','" + T32.Text + "','" + T33.Text + "','" + DTP5.Value.ToString("MM/dd/yyyy") + "','" + T34.Text + "','" + T35.Text + "','" + T36.Text + "')"; 
        comm.ExecuteNonQuery(); 
        ABC.Close(); 
        MessageBox.Show("Adaugat cu succes!"); 


      } 

也嘗試使用參數化查詢。