Exemple #1
0
        /// <summary>
        /// Executes the sql query and returns the data results
        /// </summary>
        /// <typeparam name="T">Data type in the main from clause (queryModel.MainFromClause.ItemType)</typeparam>
        protected IEnumerable <object> GetRowResults(string sql)
        {
            IEnumerable <object> results;
            OleDbDataReader      data = null;

            var conn    = ExcelUtilities.GetConnection(_args);
            var command = conn.CreateCommand();

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                command.CommandText = sql.ToString();
                try { data = command.ExecuteReader(); }
                catch (OleDbException e)
                {
                    if (e.Message.Contains(_args.WorksheetName))
                    {
                        throw new DataException(
                                  string.Format("'{0}' is not a valid worksheet name in file {3}. Valid worksheet names are: '{1}'. Error received: {2}",
                                                _args.WorksheetName, string.Join("', '", ExcelUtilities.GetWorksheetNames(_args.FileName).ToArray()), e.Message, _args.FileName), e);
                    }
                }

                var columns = ExcelUtilities.GetColumnNames(data);
                LogColumnMappingWarnings(columns);
                results = GetRowResults(data, columns);
            }
            finally
            {
                command.Dispose();

                if (!_args.UsePersistentConnection)
                {
                    conn.Dispose();
                    _args.PersistentConnection = null;
                }
            }

            return(results);
        }
Exemple #2
0
        /// <summary>
        /// Executes the sql query and returns the data results
        /// </summary>
        /// <typeparam name="T">Data type in the main from clause (queryModel.MainFromClause.ItemType)</typeparam>
        /// <param name="queryModel">Linq query model</param>
        protected IEnumerable <object> GetDataResults(SqlParts sql, QueryModel queryModel)
        {
            IEnumerable <object> results;
            OleDbDataReader      data = null;

            var conn    = ExcelUtilities.GetConnection(_args);
            var command = conn.CreateCommand();

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                command.CommandText = sql.ToString();
                command.Parameters.AddRange(sql.Parameters.ToArray());
                try { data = command.ExecuteReader(); }
                catch (OleDbException e)
                {
                    if (e.Message.Contains(_args.WorksheetName))
                    {
                        throw new DataException(
                                  string.Format("'{0}' is not a valid worksheet name in file {3}. Valid worksheet names are: '{1}'. Error received: {2}",
                                                _args.WorksheetName, string.Join("', '", ExcelUtilities.GetWorksheetNames(_args.FileName).ToArray()), e.Message, _args.FileName), e);
                    }
                    if (!CheckIfInvalidColumnNameUsed(sql))
                    {
                        throw e;
                    }
                }

                var columns = ExcelUtilities.GetColumnNames(data);
                LogColumnMappingWarnings(columns);
                if (columns.Count() == 1 && columns.First() == "Expr1000")
                {
                    results = GetScalarResults(data);
                }
                else if (queryModel.MainFromClause.ItemType == typeof(Row))
                {
                    results = GetRowResults(data, columns);
                }
                else if (queryModel.MainFromClause.ItemType == typeof(RowNoHeader))
                {
                    results = GetRowNoHeaderResults(data);
                }
                else
                {
                    results = GetTypeResults(data, columns, queryModel);
                }
            }
            finally
            {
                command.Dispose();

                if (!_args.UsePersistentConnection)
                {
                    conn.Dispose();
                    _args.PersistentConnection = null;
                }
            }

            return(results);
        }