I cannot read the values returned from the database using Oracledatareader

Asked

Viewed 1,464 times

3

I’m having trouble reading the values returned from a select, on the line where I do:

OracleDataReader reader = cmd.ExecuteReader();

in the read it brings me the values correctly, but in the next line where I do reader.Read() says that the enumeration did not produce results.

How do I solve this problem?

Source

OracleConnection cnn = new OracleConnection(DataFunctions.GetDefaultConnectionString()); OracleCommand cmd = cnn.CreateCommand(); OracleDataAdapter adapter = new OracleDataAdapter();
try
{
    cnn.Open();
    cmd.Connection = cnn;
    cmd.CommandText = String.Format("select * from VI_TESTE where cd_codigo = {0}", code);
    OracleDataReader reader = cmd.ExecuteReader();

    // Ao debugar, aqui tem registros

    if (reader.Read())
    {
        // aqui não tem mais registros
        var test1 = reader.GetValue(0);
    }
}
finally
{
    cnn.Close();
}
  • Have a record only or more? When you debug, you can see even the selected values?

  • If you check if(reader.HasRows), he returns true?

  • @Gypsy Morrison Mendez, It has only one record with 5 fields, in the case cd_codigo is the table PK.

  • @Marcusvinicius yes it returns true in if(Remailer.Hasrows)

  • @filipe_21 I find it inappropriate reader for your case. I will put an answer.

  • 1

    Have you tested without being in debug? I found a similar question on Soen: http://stackoverflow.com/questions/6493955/datareader-has-rows-and-data-trying-to-read-from-it-says-no-data-is-present, apparently using debug causes Datareader to list and not return results when the code arrives on reader.GetValue(0);

  • 1

    @Marcusvinicius I think when he inspects the variable already occurs the iteration on top of the enumeration. So the enumeration enters empty inside the if.

  • @Gypsy omorrisonmendez Exactly what I thought, let’s wait for his answer.

Show 3 more comments

2 answers

0


The approach by OracleDataReader possibly bring you problems in inspection. For your case, use only OracleDataAdapter as an example below:

using (var cnn = new OracleConnection(DataFunctions.GetDefaultConnectionString())) 
{
    var cmd = cnn.CreateCommand(); 

    try
    {
        cnn.Open();
        cmd.Connection = cnn;
        cmd.CommandText = String.Format("select * from VI_TESTE where cd_codigo = {0}", code);
        var ds = new DataSet();
        var adapter = new OracleDataAdapter(cmd);
        adapter.Fill(ds);

        // Inspecione aqui ds
    }
    finally
    {
        cnn.Close();
    }
}
  • Diving into ds going to Dataset > non public Members > tableCollection > result view > Rows > result view > itemArray[5] are the values I need, but going to Ralations > result view > occurs the message that appears to getValue and getString "The enumeration did not produce results"

  • This is no problem. Isn’t the goal to recover the values? If so, you can read the values of DataSet hassle-free.

  • Now reading via dataset does not occur but the problem, thanks a lot

0

Try using the Getstring method instead of Getvalue.

The Read command checks if there is one more line, where it advances to the next line and returns true. According to documentation from msdn, dataReader always starts before the first line of data, so you can run Read before you start reading (exactly as you did). The Getvalue method, according to the documentation of msdn returns the object in its native format. Maybe the field is null or it is not able to do the conversion, and so gives error.

  • both Getvalue and Getstring burst the error of "the enumeration did not generate results" both inside and outside of Read() and Hasrows that return true, and Getname works normal by returning the column name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.