private void BuildTempFile(IEnumerable <string> columns, IDataReader dataReader, DbBulkOption option) { using (StreamWriter sw = new StreamWriter(File.OpenWrite(option.TempPath))) { while (dataReader.Read()) { sw.Write($"{option.LinePrefix ?? ""}"); int columnIndex = 0; foreach (var column in columns) { columnIndex++; var index = dataReader.GetOrdinal(column); Type fieldType = dataReader.GetFieldType(index); if (option.ColumnValueFormat != null) { option.ColumnValueFormat(sw, column, fieldType, dataReader[column]); //处理结束符 if (columnIndex > 1) { sw.Write(option.FieldTerminator); } continue; } //默认值处理 if (typeof(DateTime) == fieldType) { sw.Write($"{dataReader.GetDateTime(index).ToString(option.DateTimeFormat)}"); } else { sw.Write($"{dataReader.GetString(index)}"); } //处理结束符 if (columnIndex > 1) { sw.Write(option.FieldTerminator); } } sw.Write(option.LineTerminator); } sw.Flush(); } }
private void BuildTempFile(IEnumerable <string> columns, DataTable dataTable, DbBulkOption option) { using (StreamWriter sw = new StreamWriter(File.OpenWrite(option.TempPath))) { foreach (DataRow row in dataTable.Rows) { sw.Write($"{option.LinePrefix ?? ""}"); int columnIndex = 0; foreach (var column in columns) { columnIndex++; //处理结束符 if (columnIndex > 1) { sw.Write(option.FieldTerminator); } Type fieldType = row.Table.Columns[column].DataType; if (option.ColumnValueFormat != null) { option.ColumnValueFormat(sw, column, fieldType, row[column]); continue; } //默认值处理 if (typeof(DateTime) == fieldType) { sw.Write($"{row[column].ToDateTime().ToString(option.DateTimeFormat)}"); } else { sw.Write($"{row[column].ToNotNullString()}"); } } sw.Write(option.LineTerminator); } sw.Flush(); } }