【问题标题】:Command not properly ended命令未正确结束
【发布时间】:2016-02-22 17:17:11
【问题描述】:

这句话有什么问题?我收到错误“命令未正确结束”:

update subjectinfo set subject_name = '"
  + textBoxSubjectNameUpdate.Text
  + "' , subject_abbreviation = '"
  + textBoxSubjectAbbreviationUpdate.Text
  + "where subject_code = '"
  + textBoxSubjectCodeUpdate.Text + "'"

【问题讨论】:

  • 您只是在where 之前缺少一个空格,以及前面文本值的右单引号。您还邀请了 SQL 注入;请考虑使用绑定变量,而不是将用户输入直接放入您的语句中。
  • 除了它容易被sql注入吗?
  • 首先,使用参数化查询。其次,如果你坚持不这样做,至少使用参数化字符串
  • 谢谢...我会做出改变的..

标签: c# sql oracle


【解决方案1】:

您在 textBoxSubjectAbbreviationUpdate.Text 值之后缺少一个单引号,然后在它和 where 之间有一个空格:

update subjectinfo set subject_name = '"
  + textBoxSubjectNameUpdate.Text
  + "' , subject_abbreviation = '"
  + textBoxSubjectAbbreviationUpdate.Text
  + "' where subject_code = '"
  + textBoxSubjectCodeUpdate.Text + "'"

您还邀请了 SQL 注入;请考虑使用bind variables,而不是将用户输入直接放入您的语句中。

【讨论】:

    【解决方案2】:

    这个

    + "where subject_code = '"
    

    应该阅读

    + "' where subject_code = '"
    
       ^ quote and space here
    

    但是请使用参数。不要以这种方式构建 SQL,它会导致成功的SQL injection 攻击。

    【讨论】:

      【解决方案3】:

      在“where”的末尾和之前缺少一个单引号:

      update subjectinfo set subject_name = '"
        + textBoxSubjectNameUpdate.Text
        + "' , subject_abbreviation = '"
        + textBoxSubjectAbbreviationUpdate.Text
        + "' where subject_code = '"
        + textBoxSubjectCodeUpdate.Text + "'
      

      【讨论】:

      • 是的,我现在注意到,我不确定他是否希望“where”成为插入的一部分。我现在已经纠正了。谢谢!
      • 你最后还留下了一个流浪的单引号。
      • 该死!多任务处理! :)
      【解决方案4】:

      理想情况下,您不应在代码中使用 SQL 语句以避免 SQL 注入。

      以上具体情况,可以使用更简洁、性能负担更小的StringBuilder类来编写。

       StringBuilder sb = new StringBuilder("update subjectinfo set subject_name = '");
                  sb.Append(textBoxSubjectNameUpdate.Text);
                  sb.Append("' , subject_abbreviation = '");
                  sb.Append(textBoxSubjectAbbreviationUpdate.Text);
                  sb.Append("' where subject_code = '");
                  sb.Append(textBoxSubjectCodeUpdate.Text);
                  sb.Append("'");
      
      var script  sb.ToString()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-21
        • 2023-03-23
        • 2013-01-05
        • 1970-01-01
        相关资源
        最近更新 更多