1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| if (export != null)
{
using (this.connection = new OracleConnection(Settings.Default.DataConnectionStaging))
using (IDbCommand command = this.connection.CreateCommand())
{
command.CommandText = export.Query;
this.connection.Open();
using (IDataReader reader = command.ExecuteReader())
using (StreamWriter streamExport = new StreamWriter(export.Path, false, Encoding.UTF8))
{
// If the file need a header we write it in the first line of the file.
if (export.Header)
{
// Obtains the schema of the return table for get the headers columns names.
DataTable table = reader.GetSchemaTable();
// Write all headers in the export file.
for (int i = 0; i < reader.FieldCount; i++)
{
streamExport.Write(table.Rows[i][0].ToString());
streamExport.Write(export.Separator);
}
}
while (reader.Read())
{
// If no error with current record fields length or we don't want check the fields length.
if (export.CheckLength == false || CheckFieldsLenght(reader, export))
{
// Write all fields in the export file.
for (int i = 0; i < reader.FieldCount; i++)
{
streamExport.Write(reader[i].ToString());
streamExport.Write(export.Separator);
}
streamExport.WriteLine();
}
}
}
}
} |
Partager