Exemplo n.º 1
0
 /// <summary>
 /// Populates <paramref name="this"/> with data read asynchronously from <paramref name="csvReader"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All records from <paramref name="csvReader"/> will be read and added to the table.
 /// </para>
 /// <para>
 /// If <paramref name="this"/> has columns defined, those columns will be used when populating the data. If no columns have been defined, <paramref name="csvReader"/> must have a
 /// <see cref="HeaderRecord"/>, which is then used to define the columns for <paramref name="this"/>. If any data record has more values than can fit into the columns defined on
 /// <paramref name="this"/>, an exception is thrown.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataTable"/>.
 /// </param>
 /// <param name="csvReader">
 /// The <see cref="CsvReader"/>.
 /// </param>
 /// <returns>
 /// The number of rows added to <paramref name="this"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
 /// </returns>
 public async static Task<int> FillAsync(this DataTable @this, CsvReader csvReader)
 {
     return await @this.FillAsync(csvReader, null).ConfigureAwait(false);
 }
Exemplo n.º 2
0
        private static void CopyCSVFileToStringWriter()
        {
            #region CopyCSVFileToStringWriter

            using (var stringWriter = new StringWriter())
            {
                using (var streamReader = new StreamReader("PlanetaryData.csv"))
                using (var reader = new CsvReader(streamReader))
                using (var writer = new CsvWriter(stringWriter))
                {
                    writer.ValueSeparator = '\t';
                    writer.ValueDelimiter = '\'';

                    reader.CopyTo(writer);
                }

                Console.WriteLine(stringWriter);
            }

            #endregion
        }
Exemplo n.º 3
0
        private static async Task CopyCSVFileToStringWriterAsynchronously()
        {
            #region CopyCSVFileToStringWriterAsynchronously

            using (var stringWriter = new StringWriter())
            {
                using (var streamReader = new StreamReader("PlanetaryData.csv"))
                using (var reader = new CsvReader(streamReader))
                using (var writer = new CsvWriter(stringWriter))
                {
                    writer.ValueSeparator = '\t';
                    writer.ValueDelimiter = '\'';

                    await reader.CopyToAsync(writer);
                }

                Console.WriteLine(stringWriter);
            }

            #endregion
        }
Exemplo n.º 4
0
        private static async Task FillDataTableFromCSVFileAsynchronously()
        {
            #region FillDataTableFromCSVFileAsynchronously

            var table = new DataTable();

            using (var streamReader = new StreamReader("PlanetaryData.csv"))
            using (var reader = new CsvReader(streamReader))
            {
                await reader.ReadHeaderRecordAsync();
                await table.FillAsync(reader);
            }

            Console.WriteLine("Table contains {0} rows.", table.Rows.Count);

            #endregion
        }
Exemplo n.º 5
0
        private async static Task FillDataTableFromCSVFileThenWriteSomeToStringAsynchronously()
        {
            #region FillDataTableFromCSVFileThenWriteSomeToStringAsynchronously

            var table = new DataTable();

            using (var streamReader = new StreamReader("PlanetaryData.csv"))
            using (var reader = new CsvReader(streamReader))
            {
                await reader.ReadHeaderRecordAsync();
                await table.FillAsync(reader);
            }

            using (var stringWriter = new StringWriter())
            {
                using (var writer = new CsvWriter(stringWriter))
                {
                    await table.WriteCsvAsync(writer, false, 5);
                }

                Console.WriteLine("CSV: {0}", stringWriter);
            }

            #endregion
        }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a table in <paramref name="this"/> and populates it with data read asynchronously from <paramref name="csvReader"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All records from <paramref name="csvReader"/> will be read and added to the table.
 /// </para>
 /// <para>
 /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataSet"/>.
 /// </param>
 /// <param name="csvReader">
 /// The <see cref="CsvReader"/>.
 /// </param>
 /// <param name="tableName">
 /// The name of the table to create and add to <paramref name="this"/>
 /// </param>
 /// <returns>
 /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
 /// </returns>
 public async static Task<int> FillAsync(this DataSet @this, CsvReader csvReader, string tableName)
 {
     return await @this.FillAsync(csvReader, tableName, null).ConfigureAwait(false);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Populates <paramref name="this"/> with data read from <paramref name="csvReader"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All records from <paramref name="csvReader"/> will be read and added to the table.
 /// </para>
 /// <para>
 /// If <paramref name="this"/> has columns defined, those columns will be used when populating the data. If no columns have been defined, <paramref name="csvReader"/> must have a
 /// <see cref="HeaderRecord"/>, which is then used to define the columns for <paramref name="this"/>. If any data record has more values than can fit into the columns defined on
 /// <paramref name="this"/>, an exception is thrown.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataTable"/>.
 /// </param>
 /// <param name="csvReader">
 /// The <see cref="CsvReader"/>.
 /// </param>
 /// <returns>
 /// The number of rows added to <paramref name="this"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
 /// </returns>
 public static int Fill(this DataTable @this, CsvReader csvReader)
 {
     return @this.Fill(csvReader, null);
 }
Exemplo n.º 8
0
        private static void ReadCSVFromFileWithExplicitHeader()
        {
            #region ReadCSVFromFileWithExplicitHeader

            using (var streamReader = new StreamReader("PlanetaryData_NoHeader.csv"))
            using (var reader = new CsvReader(streamReader))
            {
                reader.HeaderRecord = new HeaderRecord("OfficialName", "NickName");

                while (reader.HasMoreRecords)
                {
                    var dataRecord = reader.ReadDataRecord();
                    Console.WriteLine("{0} is nicknamed {1}.", dataRecord["OfficialName"], dataRecord["NickName"]);
                    reader.SkipRecord();
                }
            }

            #endregion
        }
Exemplo n.º 9
0
        private async static Task ReadCSVFromFileAsynchronously()
        {
            #region ReadCSVFromFileAsynchronously

            using (var textReader = new StreamReader("PlanetaryData.csv"))
            using (var reader = new CsvReader(textReader, true))
            {
                await reader.ReadHeaderRecordAsync();

                // realistically, you'll probably want a larger buffer, but this suffices for demonstration purposes
                var buffer = new DataRecord[4];

                while (reader.HasMoreRecords)
                {
                    var read = await reader.ReadDataRecordsAsync(buffer, 0, buffer.Length);

                    for (var i = 0; i < read; ++i)
                    {
                        Console.WriteLine("{0} is nicknamed {1}.", buffer[i]["Name"], buffer[i]["Nickname"]);
                    }
                }
            }

            #endregion
        }
Exemplo n.º 10
0
        private static void ReadCSVFromFile()
        {
            #region ReadCSVFromFile

            using (var streamReader = new StreamReader("PlanetaryData.csv"))
            using (var reader = new CsvReader(streamReader))
            {
                // the CSV file has a header record, so we read that first
                reader.ReadHeaderRecord();

                while (reader.HasMoreRecords)
                {
                    var dataRecord = reader.ReadDataRecord();

                    // since the reader has a header record, we can access data by column names as well as by index
                    Console.WriteLine("{0} is nicknamed {1}.", dataRecord[0], dataRecord["Nickname"]);
                }
            }

            #endregion
        }
Exemplo n.º 11
0
        private static void ReadCSVFromStream()
        {
            #region ReadCSVFromStream

            using (var stream = new FileStream("PlanetaryData.csv", FileMode.Open))
            using (var reader = new CsvReader(stream, Encoding.UTF8))
            {
                reader.ReadHeaderRecord();

                while (reader.HasMoreRecords)
                {
                    var dataRecord = reader.ReadDataRecord();
                    Console.WriteLine("{0} is nicknamed {1}.", dataRecord["Name"], dataRecord["Nickname"]);
                }
            }

            #endregion
        }
Exemplo n.º 12
0
        private static void ReadTabDelimitedDataFromFile()
        {
            #region ReadTabDelimitedDataFromFile

            using (var streamReader = new StreamReader("PlanetaryData.tdv"))
            using (var reader = new CsvReader(streamReader))
            {
                reader.ValueSeparator = '\t';
                reader.ValueDelimiter = '\'';

                while (reader.HasMoreRecords)
                {
                    var dataRecord = reader.ReadDataRecord();
                    Console.WriteLine("{0} is nicknamed {1}.", dataRecord[0], dataRecord[dataRecord.Count - 1]);
                }
            }

            #endregion
        }
Exemplo n.º 13
0
 /// <summary>
 /// Creates a table in <paramref name="this"/> and populates it with data read from <paramref name="csvReader"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All records from <paramref name="csvReader"/> will be read and added to the table.
 /// </para>
 /// <para>
 /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataSet"/>.
 /// </param>
 /// <param name="csvReader">
 /// The <see cref="CsvReader"/>.
 /// </param>
 /// <param name="tableName">
 /// The name of the table to create and add to <paramref name="this"/>
 /// </param>
 /// <returns>
 /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
 /// </returns>
 public static int Fill(this DataSet @this, CsvReader csvReader, string tableName)
 {
     return @this.Fill(csvReader, tableName, null);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Creates a table in <paramref name="this"/> and populates it with data read from <paramref name="csvReader"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// The name of the table created and added to <paramref name="this"/> is <see cref="DefaultTableName"/>. All records from <paramref name="csvReader"/> will be read and added to the table.
 /// </para>
 /// <para>
 /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataSet"/>.
 /// </param>
 /// <param name="csvReader">
 /// The <see cref="CsvReader"/>.
 /// </param>
 /// <returns>
 /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
 /// </returns>
 public static int Fill(this DataSet @this, CsvReader csvReader)
 {
     return @this.Fill(csvReader, DefaultTableName);
 }
Exemplo n.º 15
0
        /// <summary>
        /// Populates <paramref name="this"/> with data read asynchronously from <paramref name="csvReader"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// If <paramref name="this"/> has columns defined, those columns will be used when populating the data. If no columns have been defined, <paramref name="csvReader"/> must have a
        /// <see cref="HeaderRecord"/>, which is then used to define the columns for <paramref name="this"/>. If any data record has more values than can fit into the columns defined on
        /// <paramref name="this"/>, an exception is thrown.
        /// </para>
        /// </remarks>
        /// <param name="this">
        /// The <see cref="DataTable"/>.
        /// </param>
        /// <param name="csvReader">
        /// The <see cref="CsvReader"/>.
        /// </param>
        /// <param name="maximumRecords">
        /// The maximum number of records to read and add to <paramref name="this"/>.
        /// </param>
        /// <returns>
        /// The number of rows added to <paramref name="this"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
        /// </returns>
        public async static Task<int> FillAsync(this DataTable @this, CsvReader csvReader, int? maximumRecords)
        {
            @this.AssertNotNull("@this");
            csvReader.AssertNotNull("csvReader");
            exceptionHelper.ResolveAndThrowIf(maximumRecords.GetValueOrDefault() < 0, "maximumRecordsMustBePositive");

            if (@this.Columns.Count == 0)
            {
                // table has no columns, so we need to use the CSV header record to populate them
                exceptionHelper.ResolveAndThrowIf(csvReader.HeaderRecord == null, "noColumnsAndNoHeaderRecord");

                foreach (var columnName in csvReader.HeaderRecord)
                {
                    @this.Columns.Add(columnName);
                }
            }

            var remaining = maximumRecords.GetValueOrDefault(int.MaxValue);
            var buffer = new DataRecord[16];

            while (remaining > 0)
            {
                var read = await csvReader.ReadDataRecordsAsync(buffer, 0, Math.Min(buffer.Length, remaining)).ConfigureAwait(false);

                if (read == 0)
                {
                    // no more data
                    break;
                }

                for (var i = 0; i < read; ++i)
                {
                    var record = buffer[i];
                    exceptionHelper.ResolveAndThrowIf(record.Count > @this.Columns.Count, "moreValuesThanColumns", @this.Columns.Count, record.Count);

                    var recordAsStrings = new string[record.Count];
                    record.CopyTo(recordAsStrings, 0);
                    @this.Rows.Add(recordAsStrings);
                }

                remaining -= read;
            }

            return maximumRecords.GetValueOrDefault(int.MaxValue) - remaining;
        }
Exemplo n.º 16
0
        private async static Task ReadCSVFromFileAndWriteToTabDelimitedFile()
        {
            #region ReadCSVFromFileAndWriteToTabDelimitedFile

            using (var streamReader = new StreamReader("PlanetaryData.csv"))
            using (var reader = new CsvReader(streamReader))
            using (var streamWriter = new StreamWriter("PlanetaryData_Modified.csv"))
            using (var writer = new CsvWriter(streamWriter))
            {
                writer.ValueSeparator = '\t';
                writer.ValueDelimiter = '\'';

                // realistically, you'll probably want a larger buffer, but this suffices for demonstration purposes
                var buffer = new DataRecord[4];

                while (reader.HasMoreRecords)
                {
                    var read = await reader.ReadDataRecordsAsync(buffer, 0, buffer.Length);
                    await writer.WriteRecordsAsync(buffer, 0, read);
                }
            }

            #endregion
        }
Exemplo n.º 17
0
 /// <summary>
 /// Creates a table in <paramref name="this"/> and populates it with data read asynchronously from <paramref name="csvReader"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// The name of the table created and added to <paramref name="this"/> is <see cref="DefaultTableName"/>. All records from <paramref name="csvReader"/> will be read and added to the table.
 /// </para>
 /// <para>
 /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataSet"/>.
 /// </param>
 /// <param name="csvReader">
 /// The <see cref="CsvReader"/>.
 /// </param>
 /// <returns>
 /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
 /// </returns>
 public async static Task<int> FillAsync(this DataSet @this, CsvReader csvReader)
 {
     return await @this.FillAsync(csvReader, DefaultTableName).ConfigureAwait(false);
 }
Exemplo n.º 18
0
        private static void FillDataTableFromCSVFile()
        {
            #region FillDataTableFromCSVFile

            var table = new DataTable();

            using (var streamReader = new StreamReader("PlanetaryData.csv"))
            using (var reader = new CsvReader(streamReader))
            {
                reader.ReadHeaderRecord();
                table.Fill(reader);
            }

            Console.WriteLine("Table contains {0} rows.", table.Rows.Count);

            #endregion
        }
Exemplo n.º 19
0
        /// <summary>
        /// Creates a table in <paramref name="this"/> and populates it with data read asynchronously from <paramref name="csvReader"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
        /// </para>
        /// </remarks>
        /// <param name="this">
        /// The <see cref="DataSet"/>.
        /// </param>
        /// <param name="csvReader">
        /// The <see cref="CsvReader"/>.
        /// </param>
        /// <param name="tableName">
        /// The name of the table to create and add to <paramref name="this"/>
        /// </param>
        /// <param name="maximumRecords">
        /// The maximum number of records to read and add to the <see cref="DataTable"/>.
        /// </param>
        /// <returns>
        /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
        /// </returns>
        public async static Task<int> FillAsync(this DataSet @this, CsvReader csvReader, string tableName, int? maximumRecords)
        {
            @this.AssertNotNull("@this");
            tableName.AssertNotNull("tableName");

            var table = @this.Tables.Add(tableName);
            return await table.FillAsync(csvReader, maximumRecords).ConfigureAwait(false);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Creates a table in <paramref name="this"/> and populates it with data read from <paramref name="csvReader"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>.
        /// </para>
        /// </remarks>
        /// <param name="this">
        /// The <see cref="DataSet"/>.
        /// </param>
        /// <param name="csvReader">
        /// The <see cref="CsvReader"/>.
        /// </param>
        /// <param name="tableName">
        /// The name of the table to create and add to <paramref name="this"/>
        /// </param>
        /// <param name="maximumRecords">
        /// The maximum number of records to read and add to the <see cref="DataTable"/>.
        /// </param>
        /// <returns>
        /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>).
        /// </returns>
        public static int Fill(this DataSet @this, CsvReader csvReader, string tableName, int? maximumRecords)
        {
            @this.AssertNotNull("@this");
            tableName.AssertNotNull("tableName");

            var table = @this.Tables.Add(tableName);
            return table.Fill(csvReader, maximumRecords);
        }