【问题标题】:error Incorrect syntax near in c#错误在c#中附近的语法不正确
【发布时间】:2016-11-15 16:52:35
【问题描述】:

此语句给出以下错误:

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常 附加信息:',' 附近的语法不正确。

注意:我的表有 22 个字段 感谢您的帮助

con.ConnectionString = @"Data Source =.\MYSQL; Initial Catalog = db_info; Integrated Security = True";
con.Open();
/SqlCommand com = new SqlCommand("insert into tbl_tarifmoshtari values(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16,,,,,,,)", con);
SqlCommand com1 = new SqlCommand();

com1.Parameters.AddWithValue("@p1", Convert.ToInt32(txbox_shomaremoshtari.Text));
com1.Parameters.AddWithValue("@p2", cmbo_jensiyat.Text);
com1.Parameters.AddWithValue("@p3", txbox_nam.Text);
com1.Parameters.AddWithValue("@p4", txbox_famil.Text);
com1.Parameters.AddWithValue("@p5", txbox_pedar.Text);
com1.Parameters.AddWithValue("@p6", txbox_tarikhtavalod.Text);
com1.Parameters.AddWithValue("@p7", txbox_shomareshenasname.Text);
com1.Parameters.AddWithValue("@p8", txbox_codmeli.Text);
com1.Parameters.AddWithValue("@p9", txbox_mahaltavalod.Text);
com1.Parameters.AddWithValue("@p10", txbox_serishenasname.Text);
com1.Parameters.AddWithValue("@p11", txbox_serialshenasname.Text);
com1.Parameters.AddWithValue("@p12", txbox_adresmanzel.Text);
com1.Parameters.AddWithValue("@p13", txbox_codpostimanzel.Text);
com1.Parameters.AddWithValue("@p14", Convert.ToInt32(txbox_telephonmanzel.Text));
com1.Parameters.AddWithValue("@p15", Convert.ToInt32(txbox_mobil.Text));
com1.Parameters.AddWithValue("@p16", txbox_email.Text);
com1.ExecuteNonQuery();

【问题讨论】:

  • 你要插入 23 个字段,所以这不是一个好的开始
  • 如果您没有插入所有值,则需要在查询中指定而不是添加逗号(据我所知)。该查询无效。

标签: c# sql syntax sqlcommand executenonquery


【解决方案1】:

如果您的表有 23 列并且您不想插入到所有 23 列中,请在 Insert 中提及所需的列列表。不用加逗号

Insert into tbl_tarifmoshtari 
(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16)
values
(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16)

【讨论】:

  • 谢谢亲爱的朋友
  • 我帮了你很大的忙我希望你永远健康快乐我接受你作为来自另一个国家的朋友!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多