You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL语法错误:'00'附近存在语法错误,附SQL拼接代码

排查SQL插入错误:Incorrect syntax near '00'

执行SQL插入操作时出现错误提示:Incorrect syntax near '00',以下是生成INSERT语句的C#代码:

string ssql2 = 

    @"INSERT INTO trauditmain
        (docid,
         temporarydocno,
         temporarydocdate,
         referanceno,
         referencedate,
         permanentdocno,
         permanentdocdate,
         operationlog,
         entrydate,
         userid)
VALUES      (" + txtdocid.text + @",
         " + txttemporarydocno.text + @",
         " + dtptemporarydocdate.value.tostring() + @",
         " + txtreferanceno.text + @",
         '" + dtpReferanceDate.Value.ToString() + @"',
         '" + txtPermanentDocNo.Text + @"',
         '" + dtpPermanentDocDate.Value.ToString() + @"',
'" + " Record Inserted By " + Variables.User + " on " + 
DateTime.Now + @"',
'" + DateTime.Now.Date + @"',
" + variables.userid + @")";

错误原因分析

  1. 日期字段未加单引号且格式问题:dtptemporarydocdate.value.tostring()没有用单引号包裹,当日期带时间(例如2024-05-20 14:00:00)时,SQL会把14:00当成非法语法,解析到00时触发错误,这就是报错的直接原因。
  2. 字符串字段未统一加单引号:txtdocid.text、txttemporarydocno.text等字符串类型字段如果没加单引号,会导致SQL解析混乱。
  3. 存在严重SQL注入风险:直接拼接用户输入生成SQL语句,恶意用户可以通过输入特殊字符篡改SQL逻辑,引发安全问题。

解决方案

临时修复:修正字符串拼接(不推荐长期使用)

给所有字符串/日期类型字段添加单引号,并指定SQL可识别的日期格式:

string ssql2 = 
    @"INSERT INTO trauditmain
        (docid,
         temporarydocno,
         temporarydocdate,
         referanceno,
         referencedate,
         permanentdocno,
         permanentdocdate,
         operationlog,
         entrydate,
         userid)
VALUES      ('" + txtdocid.Text + @"',
         '" + txttemporarydocno.Text + @"',
         '" + dtptemporarydocdate.Value.ToString("yyyy-MM-dd HH:mm:ss") + @"',
         '" + txtreferanceno.Text + @"',
         '" + dtpReferanceDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + @"',
         '" + txtPermanentDocNo.Text + @"',
         '" + dtpPermanentDocDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + @"',
'" + " Record Inserted By " + Variables.User + " on " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + @"',
'" + DateTime.Now.Date.ToString("yyyy-MM-dd") + @"',
" + variables.userid + @")";

注:如果docid、userid是数字类型,无需加单引号;字符串类型必须添加。

推荐方案:使用参数化查询(彻底解决语法错误+防注入)

参数化查询会自动处理数据类型和转义,从根源避免语法错误和SQL注入:

string ssql2 = @"INSERT INTO trauditmain
    (docid,
     temporarydocno,
     temporarydocdate,
     referanceno,
     referencedate,
     permanentdocno,
     permanentdocdate,
     operationlog,
     entrydate,
     userid)
VALUES      (@docid,
         @temporarydocno,
         @temporarydocdate,
         @referanceno,
         @referencedate,
         @permanentdocno,
         @permanentdocdate,
         @operationlog,
         @entrydate,
         @userid)";

// 以SqlConnection为例(根据实际数据库类型调整)
using (SqlConnection conn = new SqlConnection("你的数据库连接字符串"))
{
    conn.Open();
    using (SqlCommand cmd = new SqlCommand(ssql2, conn))
    {
        // 添加参数,自动处理类型和转义
        cmd.Parameters.AddWithValue("@docid", txtdocid.Text);
        cmd.Parameters.AddWithValue("@temporarydocno", txttemporarydocno.Text);
        cmd.Parameters.AddWithValue("@temporarydocdate", dtptemporarydocdate.Value);
        cmd.Parameters.AddWithValue("@referanceno", txtreferanceno.Text);
        cmd.Parameters.AddWithValue("@referencedate", dtpReferanceDate.Value);
        cmd.Parameters.AddWithValue("@permanentdocno", txtPermanentDocNo.Text);
        cmd.Parameters.AddWithValue("@permanentdocdate", dtpPermanentDocDate.Value);
        cmd.Parameters.AddWithValue("@operationlog", $" Record Inserted By {Variables.User} on {DateTime.Now}");
        cmd.Parameters.AddWithValue("@entrydate", DateTime.Now.Date);
        cmd.Parameters.AddWithValue("@userid", variables.userid);

        cmd.ExecuteNonQuery();
    }
}

内容的提问来源于stack exchange,提问作者Padmasinh Kadam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 19:51:06