c#
  • sql
  • oracle
  • 2016-02-22 240 views -3 likes 
    -3

    此声明有何问题?我收到错误 “命令不能正确地结束”:命令未正确结束

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

    你只是在'where'之前缺少一个空格,而对于前面的Text值则是一个闭合的单引号。您还正在邀请SQL注入;请考虑使用绑定变量,而不是将用户输入直接放入您的语句中。 –

    +0

    除了它容易受到sql注入的事实吗? –

    +0

    首先,使用参数化查询。其次,如果你坚持不这样做,至少使用参数化字符串 –

    回答

    3

    你缺少一个右单引号您textBoxSubjectAbbreviationUpdate.Text值后,再那和where之间的空间:

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

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

    1

    在年底前 “其中” 缺少一个单引号:

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

    是啊,我现在注意到,我不确定他是否想要“哪里”成为零件的插入。我现在纠正了它。谢谢! –

    +1

    最后你还留下了一个流浪的单引号。 –

    +0

    该死!多任务! :) –

    2

    + "where subject_code = '" 
    

    应该读

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

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

    0

    理想情况下,您不应该在代码中使用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() 
    
    相关问题