C#/ SQL语法不正确(C# / SQL incorrect syntax)

编程入门 行业动态 更新时间:2024-10-28 18:28:16
C#/ SQL语法不正确(C# / SQL incorrect syntax)

我不明白问题在哪里,我试着修复,搜索,我找不到问题。

','附近的语法不正确。

码:

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!"); }

I don't understand where the problem is, I try to fix, to search and I can't find the problem.

Incorrect syntax near ','.

Code:

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!"); }

最满意答案

正如已经指出的那样,Exception是由你的声明中的double引起的。 真的,你需要使用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!"); } }

最后了解异常是什么以及如何阅读它们。

As already pointed out the Exception is caused by a double , in your statement. Really you need to use Sql Parameters. Also use the using statement, and do not share connection instances (again, see the link).

// 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!"); } }

Finally understand what Exceptions are and how to read them.

更多推荐

本文发布于:2023-07-08 19:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1079981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   语法   SQL   incorrect   syntax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!