【问题标题】:Getting an exception Incorrect syntax near ','. in C# [closed]获取异常 ',' 附近的语法不正确。在 C# [关闭]
【发布时间】:2017-11-02 10:51:51
【问题描述】:

我在使用 int、date、float、varchar 数据类型的 C# 中遇到错误

听从命令

con.Open();
string cb = "insert into tran1(tno,billno,Tdate,Acno,SubTotal,TaxPer,Discountper,DiscountAmt,TaxAmount,GrandTotal,UserId,Remarks,cash,change)values(" + txtTno.Text + ",'" + txtBillNo.Text + "',"+dtpBillDate.Text+","+txtACNO.Text+","+txtSubTotal.Text+","+txtTaxPer.Text+","+txtDiscountPer.Text+","+txtDiscountAmount.Text+","+txtTaxAmt.Text+","+txtTotal.Text+","+txtUserId.Text+",'"+txtNarr.Text+"',"+txtCash.Text+",'"+txtChange.Text+"')";
cmd = new SqlCommand(cb);
cmd.Connection = con;
cmd.ExecuteReader();
if (con.State == ConnectionState.Open)
{
    con.Close();
}
con.Close();

【问题讨论】:

  • 您似乎正试图在数据库中插入一行。在本地数据库中手动尝试 SQL。很有可能是语法问题或列数与值数不匹配。
  • 对于您当前的问题并防止更多错误,请使用SqlParameters
  • 请开始执行带参数的 SQL 查询。这不仅可以保护您免受 SQL 注入,还可以更轻松地使用不同的数据类型,因为它将其转换为所需的数据。 See this MSDN Documentation
  • 强制警告:Exploits of a Mom.
  • 最有可能导致查询出错的地方就是这个dtpBillDate.Text,当然使用SqlParamaters是必须的。

标签: sql sql-server winforms


【解决方案1】:

您在 SQL 语句中的参数周围缺少引号... 例如;

values (" + txtTno.Text + ",

会做的

values ( Whatever ,

应该是

values ('" + txtTno.Text + "',

给出正确的;

values ('Whatever',

然而,正如其他人在 cmets 中所说的那样不要这样做 - 而是使用 SQL 参数,因为这既不容易出错,也更易读,并且可以让您免于 SQL 注入等令人讨厌的事情。

这看起来像;

values ( @txtTno ,

然后

cmd.Parameters.AddWithValue("@txtTno", txtTno.Text);

【讨论】:

  • 如果文本值可以转换为数字(并且实际上被视为数字数据),则可能会错过引号,所以我想应该只引用dtpBillDate.Text(并且其他一些已经被他自己引用了) )。
  • 要补充这个答案,请查看 using 语句。它将为您释放非托管资源。 docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
【解决方案2】:

错误的原因是您正在连接应该在它们周围带有' 刻度的值,例如Dates。您可以通过使用参数来解决此问题,但我建议使用以下所有方法:

  1. 如果您正在执行像 INSERT/DELETE/UPDATE 这样的非查询操作,请使用 ExecuteNonQuerynot ExecuteReader 用于从查询中读取结果。
  2. 始终在查询中使用参数,而不是字符串连接。它可以防止 sql 注入并确保您永远不会遇到包含转义字符的字符串的问题。
    • 参数是值占位符
    • 当您添加参数的值时,它应该与架构中的类型相匹配,因此请使用 System.DateTime 作为日期,int 用于 intstring 用于 varchar/nvarchar 等。
    • 始终指定SqlDbType
    • 如果适用,还要指定长度
  3. 尽量不要重复使用连接,除非必须这样做。主要原因是您通常希望尽快关闭连接,如果您有可能同时发生的多个数据驱动的事件/操作,它将保护您免受可能的竞争条件。
  4. 尽快关闭您的连接,这样您就没有打开的外部资源。
  5. 为确保 1 和 2 发生,请将您的 IDisposable 类型包装在 using 块中。
  6. 将连接字符串添加到app.configweb.config 并使用System.Configuration.ConfigurationManager.ConnectionStrings["name-here"].ConnectionString 检索它(需要引用程序集System.configuration

代码

var query = "INSERT INTO tran1(tno, billno, Tdate, Acno, SubTotal, TaxPer, Discountper, DiscountAmt, TaxAmount, GrandTotal, UserId, Remarks, cash, change) VALUES(@tno, @billno, @Tdate, @Acno, @SubTotal, @TaxPer, @Discountper, @DiscountAmt, @TaxAmount, @GrandTotal, @UserId, @Remarks, @cash, @change)";

using(var con = new SqlConnection("connection string here"))
using(var command = new SqlCommand(queryString, con))
{
  command.Parameters.Add(new SqlParameter("@tno", SqlDbType.Int){Value = int.Parse(txtTno.Text)});
  command.Parameters.Add(new SqlParameter("@billno", SqlDbType.VarChar, 100){Value = txtBillNo.Text});
  // convert your string date into a DateTime, use Parse or ParseExact with the appropriate formater and CultureInfo if applicable
  command.Parameters.Add(new SqlParameter("@Tdate", SqlDbType.Date){Value = DateTime.Parse(dtpBillDate.Text)});

  // continue to add the rest of your parameters here


  con.Open();
  command.ExecuteNonQuery();
}

【讨论】:

  • 它显示另一个错误是必须声明标量变量“@UserId”。
  • 上面的代码不完整,你必须添加所有的参数才能工作。见评论continue to add the rest of your parameters here。错误Must declare the scalar variable "@UserId"表示您没有将参数@UserId添加到参数列表中。
  • 它完成了先生......实际上的问题是UserId在这个Windows表单数据库中传递了空白......我想自动传递该软件登录的用户ID.. .. 然后我想知道如何在 Windows 窗体应用程序中添加登录用户的用户 ID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2021-03-22
  • 2020-11-03
  • 2018-10-25
  • 2013-06-26
  • 2020-02-07
相关资源
最近更新 更多