简体   繁体   中英

Error retrieving dataset with Informix function on .NET

I'm having problems retrieving the result of a function that's on Informix and called from a VB .NET code.

This is the Stack Trace:

   at IBM.Data.Informix.IfxDateTime.ValidateRange()
   at IBM.Data.Informix.IfxDataReader.internalGetIfxDateTime(Int32 i)
   at IBM.Data.Informix.IfxDataReader.GetValue(Int32 column, TypeMap typeMap)
   at IBM.Data.Informix.IfxDataReader.GetValue(Int32 i)
   at IBM.Data.Informix.IfxDataReader.GetValues(Object[] values)
   at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
   at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
   at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at ServicesDAL.Repositories.RepositorioBase.GetDatatableFromCommand(String connectionString, String commandText, List`1 parameters, CommandType commandType, String& errorMessage)

Any clue why is this happening?

And when I execute the function from the sql client everything works fine and all the results are displayed.

And this is the code:

internal static DataTable GetDatatableFromCommand(string connectionString, string commandText, List<ParameterObject> parameters, CommandType commandType, ref string errorMessage)
        {

            DataTable dt = null;
            errorMessage = null;
            IfxConnection connection = null;
            try
            {
                // Conecto con la base de datos
                connection = ConnectToDatabase(connectionString, ref errorMessage);

                // Si es distinto de null y no hay errores entonces se conecto correctamente
                if (connection != null && string.IsNullOrWhiteSpace(errorMessage))
                {
                    IfxCommand cmd = connection.CreateCommand();
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;

                    if (parameters != null)
                    {
                        IfxParameter parameter = null;
                        foreach (ParameterObject parObj in parameters)
                        {
                            parameter = new IfxParameter(parObj.Orden, parObj.Tipo);
                            parameter.Value = parObj.Valor;
                            cmd.Parameters.Add(parameter);
                        }
                    }

                    // Antes de ejecutar la llamada a la base de datos cambio el currentculture
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-ES");

                    // Creo un adaptador que le paso el comando y ya el lo parsea con el dataTable
                    IfxDataAdapter dataAdapter = new IfxDataAdapter(cmd);
                    dt = new DataTable();
                    dataAdapter.Fill(dt);
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            finally
            {
                // Cierro la conexion siempre despues de sacar los datos
                if (connection != null)
                {
                    connection.Close();
                    connection = null;
                }
            }
            return dt;
        }

Update: Could I format the date before it reaches the .net program? I mean, format the date in the informix function.

Since you're filling a blank DataTable, this is a bug or limitation in the informix ADO.NET provider. So you'll need to work around this in exactly the way you suggest:

Could I format the date before it reaches the .net program? I mean, format the date in the informix function.

Yes. Write a query using Informix SQL that invokes the function converts the data types to something that the ADO.NET provider can handle. Strings are the fallback here, and you can convert them to .NET types yourself later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM