【问题标题】:I cannot update my oracle database using c#我无法使用 c# 更新我的 oracle 数据库
【发布时间】:2016-06-01 16:19:57
【问题描述】:

我正在尝试从 c# 更新我的 oracle 数据库,但出现此错误

ORA-00933:SQL 命令未正确结束

我不知道该怎么办。

String sqlCommand = "UPDATE CLIENTI_CD ";
sqlCommand += "set nume_client=" + txtNumeC.Text+ "'";
sqlCommand += ",localit_client="+txtLocalitateC.Text+ "'";
sqlCommand +=",data_n to_date('"+txtDataN.Text+"','DDMMYYYY')";
sqlCommand += "where cod_client=" + label1.Text;

【问题讨论】:

  • 尝试在第 4 行的括号之后或第 5 行的“where”之前添加一个空格。另外,我不知道您的数据来自哪里,但它似乎对 SQL 注入开放如果你只是直接从文本框喂食。

标签: c# oracle ora-00933


【解决方案1】:

异常的直接原因是:

省略"'"s:

  sqlCommand += "set nume_client='" + txtNumeC.Text + "'";
  sqlCommand += ",localit_client='" + txtLocalitateC.Text + "'";

省略"=":

  sqlCommand +=",data_n = to_date('" + txtDataN.Text+"','DDMMYYYY')"; // please, notice "="

然而,最好的方法是使用参数化查询:

  String sqlCommand = 
    @"update CLIENTI_CD
         set nume_client = :prm_nume_client,
             localit_client = :prm_localit_client,
             data_n = to_date(:prm_date, 'DDMMYYYY')
       where cod_client = :prm_cod_client";

当您不能/不想放置参数化查询时,至少使用一个格式化

  String sqlCommand = String.Format(
     @"update CLIENTI_CD
          set nume_client = '{0}',
              localit_client = '{1}',
              data_n = to_date('{2}', 'DDMMYYYY')
        where cod_client = {3}",
    txtNumeC.Text, 
    txtLocalitateC.Text, 
    txtDataN.Text, 
    label1.Text);

【讨论】:

  • @Catalin Duceac:那么你必须调试:执行前sqlCommand字符串是什么?
  • 现在我得到“ORA-01756:引用的字符串没有正确终止”
  • @Catalin Duceac:我明白了。看看我的编辑,你还有另一个语法错误
  • 调试 sql 命令时,在对数据库执行查询之前将查询视为一个完整的字符串会很有帮助。
  • @DmitryBychenko 非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2011-08-16
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
相关资源
最近更新 更多