Skip to Main Content

ODP.NET

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

System.IndexOutOfRangeException in Oracle.ManagedDataAccessCore (2.19.80)

4252326Jul 12 2020 — edited Jul 29 2020

Enviroment

  • Platform: .NET Core 3.1 Console Application
  • Oracle Nuget: Oracle.ManagedDataAccessCore (2.19.80)

Steps

1. Open a connection

2. Fill a data table by a query

3. Change the table's structure e.g. add a new column

4. Fill the datatable again, an System.IndexOutOfRangeException will be throwed

5. Notice that I've set the AddToStatementCache as false

Exception

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

This exception was originally thrown at this call stack:

    Oracle.ManagedDataAccess.Client.OracleDataReader.GetMinSchemaTable()

    Oracle.ManagedDataAccess.Client.OracleDataReader.IsFillReader.set(bool)

    Oracle.ManagedDataAccess.Client.OracleDataAdapter.Fill(System.Data.DataTable[], int, int, System.Data.IDbCommand, System.Data.CommandBehavior)

    System.Data.Common.DbDataAdapter.Fill(System.Data.DataTable)

    OracleTest.Extensions.GetTableBaseSchema(Oracle.ManagedDataAccess.Client.OracleConnection, string) in Program.cs

    OracleTest.Program.Main(string[]) in Program.cs

Code Example

using Oracle.ManagedDataAccess.Client;

using System;

using System.Data;

namespace OracleTest

{

    public static class Extensions

    {

        public static OracleCommand CreateCommandEx(this OracleConnection connection)

        {

            var command = connection.CreateCommand();

            command.BindByName = true;

            command.AddToStatementCache = false;

            command.InitialLONGFetchSize = -1;

            return command;

        }

        public static DataTable GetTableBaseSchema(this OracleConnection oracleConnection, string tableName)

        {

            var sql = $"select * from \"{tableName}\" where rownum < 0";

            using (var command = oracleConnection.CreateCommandEx())

            {

                command.CommandText = sql;

                DataTable dataTable = new DataTable();

                using (var adapter = CreateDataAdapter())

                {

                    adapter.SelectCommand = command;

                    adapter.Fill(dataTable);

                    return dataTable;

                }

            }

        }

        public static void AddColumn(this OracleConnection oracleConnection, string tableName, string columnName)

        {

            var sql = $"ALTER TABLE \"{tableName}\" ADD \"{columnName}\" nvarchar2(2000)  DEFAULT NULL NULL";

            using (var command = oracleConnection.CreateCommandEx())

            {

                command.CommandText = sql;

                command.ExecuteNonQuery();

            }

        }

        private static OracleDataAdapter CreateDataAdapter()

        {

            return new OracleDataAdapter();

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            var connectionStr = @"YOUR CONENCTION STRING HERE";

            var oracleConnection = new OracleConnection(connectionStr);

            oracleConnection.Open();

            // Execute query, make sure that the oldTable exists in your database.

            oracleConnection.GetTableBaseSchema("oldTable");

            // Alter table column

            oracleConnection.AddColumn("oldTable", "c3");

            // Execute query again, and an exception will be throwed.

            oracleConnection.GetTableBaseSchema("oldTable");

        }

    }

}

This post has been answered by Mark Williams on Jul 13 2020
Jump to Answer

Comments

EdStevens

This really has nothing to do with SQL Developer (the subject of this forum). It is a much better fit in SQL & PL/SQL.
And just as a side comment, storing 'age' as data is a flawed design. The 'age' of everyone and every thing is increasing by the day, if not by the second. What is your plan to keep 'age' current? Better to store 'date of birth' and the calculate 'age' when needed at run-time.

User_H3J7U

The 'age' of everyone and every thing is increasing by the day, if not by the second.
Sometimes the age stops increasing.
изображение.png

EdStevens

Pour me one!
Of course, that's not actually the age, but the amount of time it was 'aged' in the barrel before bottling.

1 - 3

Post Details

Added on Jul 12 2020
4 comments
3,319 views