简体   繁体   English

使用Oracle参数查询不返回结果11g

[英]Query using Oracle Parameter returns no result 11g

A simple select statement using variable binding gives no rows?? 使用变量绑定的简单选择语句不提供行?

  OracleConnection con = new OracleConnection(constr);
  con.Open();
 OracleCommand cmd = new OracleCommand();
  cmd.Connection = con;
  cmd.CommandText = "select country_name from hr.countries where country_id = :country_id;

  OracleParameter p_country_id = new OracleParameter();
  p_country_id.OracleDbType = OracleDbType.Varchar2;

  p_country_id.Value = "UK";

  cmd.Parameters.Add(p_country_id);

  OracleDataReader dr = cmd.ExecuteReader();

  if (dr.Read())
  {} ---> no rows 

tried adding parameterName ,direction,size still result is 0??? 尝试添加parameterName,方向,大小仍然结果为0 ???

Any help?? 有帮助吗?

I believe this is all correct but it is written in NotePad. 我相信这都是正确的,但是它是用NotePad编写的。 At the very least it should get you on the right track. 至少它应该使您走上正确的道路。

using (OracleConnection con = new OracleConnection(constr))
{
    con.Open();
    using (OracleCommand cmd = con.CreateCommand())
    {
        cmd.CommandText = "select country_name from hr.countries where country_id = :country_id";
        cmd.Parameters.Add("country_id", "UK")

        OracleDataReader dr = cmd.ExecuteReader();

        if (dr.Read()) 
       { 
            // You code here
       }
    }
}

NOTE: I put the using statements in there because this is always recommended when executing database queries. 注意:我把using语句放在那里,因为在执行数据库查询时总是建议这样做。 If an exception occurs the using statement will guarantee your database connection is still closed. 如果发生异常,using语句将确保您的数据库连接仍然关闭。

You forgot to give your parameter a name. 您忘记给参数命名。 Do so and it should work: 这样做,它应该可以工作:

p_country_id.ParameterName = "country_id";

Thanks for responding guys, My unit test project app.config was pointing to wrong DB schema :( 感谢您的答复,我的单元测试项目app.config指向错误的数据库模式:(

Grrrrrrrrrrrrr! rr! can't get anything stupid then this :) i will blame on my flu :) 不能得到任何愚蠢的东西,然后这:)我会怪我的流感:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM