private void WriteTable(string sql) { using var status = WaitStatus.StartRows(Path.GetFileName(_filePath)); var fileMode = _truncateExistingFile ? FileMode.Create : FileMode.Append; using var stream = File.Open(_filePath, fileMode, FileAccess.Write, FileShare.None); using var writer = new StreamWriter(stream, _fileEncoding); using var statement = _notebook.Prepare(sql); var args = statement.GetArgs(_env.Vars); statement.ExecuteStream(args, OnHeader, OnRow, CancellationToken.None); void OnHeader(List <string> columnNames) { if (_headerRow) { writer.WriteLine(string.Join(_separator, columnNames.Select(c => CsvUtil.QuoteCsv(c, _separator)))); } } void OnRow(object[] row) { _cancel.ThrowIfCancellationRequested(); CsvUtil.WriteCsvLine(writer, _separator, row); status.IncrementRows(); } }
private void WriteTable(SimpleDataTable sdt) { using var status = WaitStatus.StartRows(Path.GetFileName(_filePath)); var fileMode = _truncateExistingFile ? FileMode.Create : FileMode.Append; using var stream = File.Open(_filePath, fileMode, FileAccess.Write, FileShare.None); using var writer = new StreamWriter(stream, _fileEncoding); if (_headerRow) { writer.WriteLine(string.Join(_separator, sdt.Columns.Select(c => CsvUtil.QuoteCsv(c, _separator)))); } CsvUtil.WriteCsv(sdt.Rows, writer, status.IncrementRows, _separator, _cancel); }