How to get response form Oracle using C#?

I'm new in Oracle and trying to execute the next SQL request using C#

try
{
    connection.Open();
    OracleCommand cmd = new OracleCommand();
    cmd.Connection = connection;
    cmd.CommandText = "select count(*) from agreements";
    cmd.CommandType = CommandType.Text;
    OracleDataReader dr = cmd.ExecuteReader();
    dr.Read();
}

I've read Oracle documentation, I've tried to use

var response = dr.GetString(0);

But it always returns exception

Specified cast is not valid.

Does somebody know how I can solve it ? Thanks for your answers!

Jon Skeet
people
quotationmark

Given that you're querying count(*), you'd be better using ExecuteScalar rather than ExecuteReader(), for one thing. Next, the result will be an integer, which is why GetString() fails.

Change it to:

int count = (int) cmd.ExecuteScalar();

(I'd also strongly advise using using statements for your connection, command, and any readers you'd normally create.)

people

See more on this question at Stackoverflow