Esempio n. 1
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
        }
Esempio n. 2
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
        }
Esempio n. 3
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
        }