【问题标题】:OracleCommand not ended properlyOracleCommand 未正确结束
【发布时间】:2017-05-19 19:11:08
【问题描述】:

我有一个查询来检查用户是否存在于数据库中。当我通过OracleCommand 检查此查询时,它会导致异常并说command 没有正确结束。

这里是查询:

select count(*) as user_exists
 from users
  where upper(u_name) = upper('name')
  and u_password = DB_PKG_ACCESS.f_encrypt('pass')
  and (expiration_date is null or (expiration_date is not null and trunc(expiration_date) >= trunc(sysdate)));

这里是command

OracleCommand cmd = new OracleCommand("select count(*) as user_exists from users where upper(u_name) = upper('name')"
       +" and u_password = DB_PKG_ACCESS.f_encrypt('pass')"
       +" and(expiration_date is null or(expiration_date is not null and trunc(expiration_date) >= trunc(sysdate)));", con);
cmd.Parameters.Add(new OracleParameter("name", textBox1.Text));
cmd.Parameters.Add(new OracleParameter("pass", textBox2.Text));

我哪里错了?

【问题讨论】:

  • 我冒昧地猜测您应该在这里删除结束的分号trunc(sysdate)));",
  • 现在可以使用了。为什么不在末尾写分号?
  • Harun - OracleCommand 不打算与多个语句一起使用。 ; 旨在分隔语句,因此 OracleCommand 不需要(甚至不接受)它。
  • 谢谢你的解释:)
  • Harun,您将 SQL 语法与 SQLPlus(脚本)语法混淆了。 SQLPlus 脚本及其符号(例如“;”)和关键字只能与 SQLPlus、SQL Developer、Oracle Developer Tools for Visual Studio 和其他工具一起使用。不在 API 中。

标签: c# sql oracle odp.net


【解决方案1】:

尝试像这样从 OracleCommand 中的语句末尾取出分号

OracleCommand cmd = new OracleCommand("select count(*) as user_exists from users where upper(u_name) = upper('name')"
       +" and u_password = DB_PKG_ACCESS.f_encrypt('pass')"
       +" and(expiration_date is null or(expiration_date is not null and trunc(expiration_date) >= trunc(sysdate)))   ", con);

这对我来说一直是个问题......

HTH

【讨论】:

    【解决方案2】:

    尝试删除查询中的分号(命令对象自动结束)

    OracleCommand cmd = new OracleCommand("select count(*) as user_exists from users where upper(u_name) = upper('name')"
       +" and u_password = DB_PKG_ACCESS.f_encrypt('pass')"
       +" and(expiration_date is null or(expiration_date is not null and trunc(expiration_date) >= trunc(sysdate))); <==== Here remove it", con);
    

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 2018-10-25
      • 2016-09-21
      相关资源
      最近更新 更多