Why Do I get OutOfRange Exception in GetOrdinal Function of this CLOB field?

六月ゝ 毕业季﹏ 提交于 2019-11-28 09:39:36

问题


This is the sample of my code. The field FUNCTION_SCRIPT is a CLOB field (the only CLOB field) in my table IS_FUNCTION

public void ReadFunction(string FName, out string fContent) {
    OracleCommand command = _connection.CreateCommand();
    OracleTransaction transaction = _connection.BeginTransaction();
    command.Transaction = transaction;
    command.CommandText = "SELECT TO_CLOB(TO_NCLOB(FUNCTION_SCRIPT)) FROM IS_FUNCTION where FNAME=:fName ";
    command.Parameters.Add("FName", OracleType.NVarChar).Value = FName;
    OracleDataReader odr = command.ExecuteReader();
    int temp = odr.GetOrdinal("FUNCTION_SCRIPT");
    OracleLob myLob = odr.GetOracleLob(temp);
    fContent = (String)myLob.Value;
    odr.close();
}

I get an out of range exception when temp = odr.GetOrdinal("FUNCTION_SCRIPT") statement is executed. Have no idea why? I have been trying to read this CLOB field for few hours now. This is the closest I have come. Your help would be highly appreciated.

p.s. Could it be that my SELECT statement is problematic? I have been taking code from different references.


回答1:


Thanks for all the suggestions and helps. I found that my problem was resolved by adding a

 if(odr.Read())
            {
                int temp = odr.GetOrdinal("FUNCTION_SCRIPT");
                OracleLob myLob = odr.GetOracleLob(temp);
                fContent = (String)myLob.Value;
            }

In other words, I was missing the statement odr.Read after the ExecuteReader() statement.




回答2:


Try this

command.CommandText = "SELECT TO_CLOB(TO_NCLOB(FUNCTION_SCRIPT)) AS FUNCTION_SCRIPT FROM IS_FUNCTION where FNAME=:fName ";

Right now your query does not have a name because you've applied a function to the name so you need to ALIAS it to give it a name e.g AS FUNCTION_SCRIPT

UPDATE

OracleDataReader odr = command.ExecuteReader();
if(odr.HasRows)
{
  int temp = odr.GetOrdinal("FUNCTION_SCRIPT");
  OracleLob myLob = odr.GetOracleLob(temp);
  fContent = (String)myLob.Value;
}
else
   throw new Exception("No rows returned from database");


来源:https://stackoverflow.com/questions/12574341/why-do-i-get-outofrange-exception-in-getordinal-function-of-this-clob-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!