Oracle.DataAccess.Client.OracleException ORA-01008:并非所有变量都绑定在Oracle.DataAccess.Client

时间:2014-01-10 15:52:46

标签: c# asp.net oracle

我遇到了这个错误,我无法解释:

  

Oracle.DataAccess.Client.OracleException ORA-01008:不是所有变量   约束于   Oracle.DataAccess.Client.OracleException.HandleErrorHelper(的Int32   errCode,OracleConnection conn,IntPtr opsErrCtx,OpoSqlValCtx *   pOpoSqlValCtx,Object src,String procedure,Boolean bCheck)at   Oracle.DataAccess.Client.OracleCommand.ExecuteReader(布尔重新查询,   布尔fillRequest,CommandBehavior行为)at   Oracle.DataAccess.Client.OracleCommand.ExecuteReader()at   gestao.GestaoDefault.GeraSqlDataSourceDropDownDisciplinasSelectCommand(的Int32   selectValue)in   c:\ inetpub \ MiniSites \ inscricoes \ gestao \ Default.aspx.cs:第501行

我的查询:

SELECT DISTINCT
       cse.t_tbdiscip.cd_discip,
       cse.t_tbdiscip.ds_discip
  FROM cse.t_tbdiscip, cse.t_turma, cse.t_plandisc
 WHERE cse.t_plandisc.cd_curso = :CD_CURSO
   AND cse.t_turma.cd_curso = :CD_CURSO
   AND cse.t_plandisc.cd_activa = 'S'
   AND cse.t_turma.estado = 'S'
   AND cse.t_tbdiscip.cd_discip = cse.t_plandisc.cd_discip
   AND cse.t_tbdiscip.cd_discip = cse.t_turma.cd_discip
   AND cse.t_turma.cd_a_s_cur IS NOT NULL
   AND cse.t_turma.cd_lectivo = :CD_LECTIVO
 ORDER BY cse.t_tbdiscip.ds_discip

正如您所看到的,我必须对查询进行变革。 我的代码:

OracleConnection oracleSqlConnection = new OracleConnection(ConOracleString);
try
{
    // Just to see if any value is passed to the query
    LiteralMensagens.Text += String.Format("Curso : " + curso + "<br /><br />");
    LiteralMensagens.Text += String.Format("Lectivo : " + lectivo + "<br /><br />");
    LiteralMensagens.Text += String.Format("query : " + query + "<br /><br />");

    oracleSqlConnection.Open();
    OracleCommand cmdOracle = new OracleCommand(query, oracleSqlConnection);
    cmdOracle.CommandType = CommandType.Text;

    cmdOracle.Parameters.Clear();
    cmdOracle.Parameters.Add(":CD_CURSO", OracleDbType.Decimal).Value = curso;
    cmdOracle.Parameters.Add(":CD_LECTIVO", OracleDbType.Varchar2).Value = lectivo;

    OracleDataReader oracleDataReader = cmdOracle.ExecuteReader();
    while (oracleDataReader.Read())
    {
        LiteralMensagens.Text += string.Format((String)oracleDataReader["DS_DISCIP"]);
    }
}
catch (Exception ex)
{

从我的代码中我绑定了我的vars。

来自Oracle DB:

CD_LECTIVO- VARCHAR2(7)

CD_CURSO- NUMBER(4)

我在这里查了一下,我想我正在使用正确的类型http://msdn.microsoft.com/en-us/library/yk72thhd.aspx

有什么不对?

3 个答案:

答案 0 :(得分:3)

问题解决了...... 补充一点:

cmd.BindByName = true;

发现这一点:similar issue on stackoverflow

答案 1 :(得分:0)

您应该更改这样的代码(省略参数名称中的':'):

// ...
cmdOracle.Parameters.Add("CD_CURSO", OracleDbType.Decimal).Value = curso;
cmdOracle.Parameters.Add("CD_LECTIVO", OracleDbType.Varchar2).Value = lectivo;
//...

答案 2 :(得分:0)

现在仍然没有工作真的很奇怪...我正在限制2个变量,如果我评论其中一个并在查询中插入值(不是mather一个)它完美地工作... cmdOracle.Parameters.Add不接受超过1个值...这对任何人都有意义吗?

这可以使用查询字符串上的var替换“:CURSO”:

String query = String.Format("SELECT DISTINCT CSE.T_TBDISCIP.CD_DISCIP, CSE.T_TBDISCIP.DS_DISCIP " +
"FROM CSE.T_TBDISCIP, CSE.T_TURMA, CSE.T_PLANDISC " +
"WHERE (CSE.T_PLANDISC.CD_CURSO = " + curso + ") " +
"AND (CSE.T_TURMA.CD_CURSO = " + curso + ") " +
"AND (CSE.T_PLANDISC.CD_ACTIVA='S') " +
"AND (CSE.T_TURMA.ESTADO='S') " +
"AND (CSE.T_TBDISCIP.CD_DISCIP=CSE.T_PLANDISC.CD_DISCIP) " +
"AND (CSE.T_TBDISCIP.CD_DISCIP=CSE.T_TURMA.CD_DISCIP) " +
"AND (CSE.T_TURMA.CD_A_S_CUR IS NOT NULL) " +
"AND (CSE.T_TURMA.CD_LECTIVO = :CD_LECTIVO) " +
"ORDER BY CSE.T_TBDISCIP.DS_DISCIP");