Exemple #1
0
        public void ShouldGetDefaultSchemaForCSVFile_GetValue()
        {
            FlatFileDataReader dataReader = GetFlatFileReaderWithDefaultSchema();

            Assert.IsTrue(dataReader.Read(), "The first record could not be read.");
            Assert.AreEqual(1, dataReader.GetValue <int>("Id"), "The wrong 'Id' was retrieved for 'Bob'.");
            Assert.AreEqual("Bob", dataReader.GetValue <string>("Name"), "The wrong 'Name' was retrieved for 'Bob'.");
            Assert.AreEqual(new DateTime(2018, 07, 03), dataReader.GetValue <DateTime>("CreatedOn"), "The wrong 'CreatedOn' was retrieved for 'Bob'.");
            Assert.IsTrue(dataReader.GetValue <bool>("IsActive"), "The wrong 'IsActive' was retrieved for 'Bob'");
            Assert.AreEqual(10, dataReader.GetValue <int?>("VisitCount"), "The wrong 'VisitCount' was retrieved for 'Bob'.");
            Assert.AreEqual(new Guid("DC3A6AE3-00C8-4884-AC0F-F61EB769DFEB"), dataReader.GetValue <Guid?>("UniqueId"), "The wrong 'UniqueId' was retrieved for 'Bob'.");
            Assert.AreEqual(DayOfWeek.Wednesday, dataReader.GetValue <DayOfWeek>("FavoriteDay"), "The wrong 'FavoriteDay' was retrieved for 'Bob'.");

            Assert.IsTrue(dataReader.Read(), "The second record could not be read.");
            Assert.AreEqual(2, dataReader.GetValue <int>("Id"), "The wrong 'Id' was retrieved for 'Susan'.");
            Assert.AreEqual("Susan", dataReader.GetValue <string>("Name"), "The wrong 'Name' was retrieved for 'Susan'.");
            Assert.AreEqual(new DateTime(2018, 07, 04), dataReader.GetValue <DateTime>("CreatedOn"), "The wrong 'CreatedOn' was retrieved for 'Susan'.");
            Assert.IsFalse(dataReader.GetValue <bool>("IsActive"), "The wrong 'IsActive' was retrieved for 'Susan'");
            Assert.AreEqual(null, dataReader.GetValue <int?>("VisitCount"), "The wrong 'VisitCount' was retrieved for 'Susan'.");
            Assert.AreEqual(new Guid("{24C250EB-87C9-45DE-B01F-71A7754C6AAD}"), dataReader.GetValue <Guid?>("UniqueId"), "The wrong 'UniqueId' was retrieved for 'Susan'.");
            Assert.AreEqual(DayOfWeek.Friday, dataReader.GetValue <DayOfWeek>("FavoriteDay"), "The wrong 'FavoriteDay' was retrieved for 'Susan'.");

            Assert.IsFalse(dataReader.Read(), "Too many records were read.");
        }
        private object ImportText()
        {
            var fileName = Project.Current.MapPath(FileName);

            if (string.IsNullOrWhiteSpace(fileName) || !File.Exists(fileName))
            {
                throw new FileNotFoundException($"File '{fileName}' not found.");
            }

            var schema = new FixedLengthSchema();

            if (Columns != null)
            {
                foreach (var column in Columns)
                {
                    var definition = CreateColumnDefinition(column);
                    var window     = new Window(column.ColumnLength);

                    if (column.FillCharacter.HasValue)
                    {
                        window.FillCharacter = column.FillCharacter.Value;
                    }
                    if (column.Alignment.HasValue)
                    {
                        window.Alignment = column.Alignment.Value;
                    }
                    if (column.TruncationPolicy.HasValue)
                    {
                        window.TruncationPolicy = column.TruncationPolicy.Value;
                    }

                    schema.AddColumn(definition, window);
                }
            }

#pragma warning disable IDE0017 // Simplify object initialization
            var options = new FixedLengthOptions();
#pragma warning restore IDE0017 // Simplify object initialization

            options.FillCharacter      = FillCharacter;
            options.HasRecordSeparator = !NoRecordSeparator;
            if (RecordSeparator != null)
            {
                options.RecordSeparator = RecordSeparator;
            }
            options.IsFirstRecordHeader = !NoHeaderRow;
            options.Alignment           = Alignment;
            if (!string.IsNullOrWhiteSpace(Culture))
            {
                options.FormatProvider = new CultureInfo(Culture);
            }

            var readerOptions = new FlatFileDataReaderOptions()
            {
                IsDBNullReturned    = true,
                IsNullStringAllowed = true
            };

            using var reader = new StreamReader(File.OpenRead(fileName));
            var csvReader  = new FixedLengthReader(reader, schema, options);
            var dataReader = new FlatFileDataReader(csvReader, readerOptions);

            var resultReader = new DataReaderWrapper(dataReader, new DataReaderWrapper.DataReaderWrapperParameters()
            {
                Columns            = this.SelectColumns,
                SkipColumns        = this.SkipColumns,
                IgnoreReaderErrors = this.IgnoreReaderErrors,
                CloseAction        = () =>
                {
                    reader.Dispose();
                }
            });

            if (AsDataReader)
            {
                return(resultReader);
            }
            else
            {
                var table = new DataTable("TextData");
                table.Load(resultReader);

                return(table);
            }
        }
        private object ImportText()
        {
            var fileName = Project.Current.MapPath(FileName);

            if (string.IsNullOrWhiteSpace(fileName) || !File.Exists(fileName))
            {
                throw new FileNotFoundException($"File '{fileName}' not found.");
            }

            if (Columns == null && NoHeaderRow)
            {
                throw new Exception($"{nameof(NoHeaderRow)} requires providing {nameof(Columns)} schema.");
            }

            var schema = new SeparatedValueSchema();

            if (Columns != null)
            {
                foreach (var column in Columns)
                {
                    var definition = CreateColumnDefinition(column);
                    schema.AddColumn(definition);
                }
            }

            var options = new SeparatedValueOptions();

            if (Separator != null)
            {
                options.Separator = Separator;
            }
            if (RecordSeparator != null)
            {
                options.RecordSeparator = RecordSeparator;
            }
            options.Quote = Quote;
            options.IsFirstRecordSchema = !NoHeaderRow;
            options.PreserveWhiteSpace  = PreserveWhiteSpace;
            if (!string.IsNullOrWhiteSpace(Culture))
            {
                options.FormatProvider = new CultureInfo(Culture);
            }

            var readerOptions = new FlatFileDataReaderOptions()
            {
                IsDBNullReturned    = true,
                IsNullStringAllowed = true
            };

            var reader     = new StreamReader(File.OpenRead(fileName));
            var csvReader  = Columns != null ? new SeparatedValueReader(reader, schema, options) : new SeparatedValueReader(reader, options);
            var dataReader = new FlatFileDataReader(csvReader, readerOptions);

            var resultReader = new DataReaderWrapper(dataReader, new DataReaderWrapper.DataReaderWrapperParameters()
            {
                Columns            = this.SelectColumns,
                SkipColumns        = this.SkipColumns,
                IgnoreReaderErrors = this.IgnoreReaderErrors,
                CloseAction        = () =>
                {
                    reader.Dispose();
                }
            });

            if (AsDataReader)
            {
                return(resultReader);
            }
            else
            {
                var table = new DataTable("TextData");
                table.Load(resultReader);

                return(table);
            }
        }