Exemplo n.º 1
0
        public void OutputQueryToCsv()
        {
            ICsvWriter csv     = CsvWriter.Create(path: _root + "output_from_table.csv", queryString: "SELECT * FROM dbo.TestTable", connectionString: _connectionString);
            bool       success = csv.OutputToCsv();
            string     error   = csv.Error;

            csv.Dispose();

            if (error != null)
            {
                throw new Exception(error);
            }

            Assert.IsTrue(success);
        }
Exemplo n.º 2
0
        public async Task WriteAsync_without_header_works()
        {
            // arrange
            var stringBuilder = new StringBuilder();
            var csv           = CsvWriter.Create(stringBuilder);

            csv.IncludeHeader = false;

            var rows = new[]
            {
                new { a = "hello", b = "world" },
                new { a = "foo", b = "bar" },
                new { a = "fizz", b = "buzz" },
                new { a = "with \"quotes\"", b = "with\rreturns\nand newlines" },
                new { a = string.Empty, b = "and empty string" },
            };

            // act
            await csv.WriteAsync(rows);

            // assert
            Assert.NotEmpty(stringBuilder.ToString());

            int i = 0;

            using (var reader = new StringReader(stringBuilder.ToString()))
                using (var csvReader = new CsvReader(reader, false))
                {
                    Assert.All(csvReader, currentCsvRow =>
                    {
                        Assert.Equal(rows[i].a, currentCsvRow[0]);
                        Assert.Equal(rows[i].b, currentCsvRow[1]);
                        i++;
                    });
                }

            Assert.Equal(rows.Length, i);
        }