简体   繁体   English

C#/ SQL语法不正确

[英]C# / SQL incorrect syntax

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

You have two commas successively. 您先后有两个逗号。 Remove the same. 删除相同。

在此处输入图片说明

Suggestion : Please use parameterized queries. 建议 :请使用参数化查询。

As already pointed out the Exception is caused by a double , in your statement. 前面已经指出的例外是由双引起的,在你的声明。 Really you need to use Sql Parameters. 确实需要使用Sql参数。 Also use the using statement, and do not share connection instances (again, see the link). 还请使用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!");
    }
}

Finally understand what Exceptions are and how to read them. 最后了解什么是异常以及如何读取它们。

this is right query try it 这是正确的查询试试

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


            }

Also try to use parameterized queries. 也尝试使用参数化查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM