2
static public void ConnectAndQuery()
        {

            string connectionString = GetConnectionString();
            using (OracleConnection conn = new OracleConnection())
            {
                conn.ConnectionString = connectionString;
                conn.Open();
                Console.WriteLine("State: " + conn.State);
                Console.WriteLine("Connection String: " + conn.ConnectionString);

                OracleCommand command = conn.CreateCommand();
                string sql = "SELECT * FROM users";
                command.CommandText = sql;

                OracleDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string myField = (string)reader["MYFIELD"];
                    Console.WriteLine(myField);
                }
            }

        }

The connection is established and working open but I get IndexOutOfRangeException when trying to acquire the data from the DB. The exception is caught on

string myField = (string)reader["MYFIELD"];

I looked for info about the OracleDataReader and the command in order to understand the reader, but... does it store the data acquired in an array or any other sequence ? Why am I getting the IndexOutOfRangeException and why does the reader require an argument in the [] brackets?

1 Answer 1

2

You probably do not have a field that is called MYFIELD. Remember that the field names are case sensitive... Try using an index instead i.e. reader[0]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.