Azavea.NijPredictivePolicing.Common.Data.FileWriterHelpers.WriteDataTable C# (CSharp) Method

WriteDataTable() public static method

Helper function for writing a datatable to a file
public static WriteDataTable ( IDataFileWriter writer, DataTable dt ) : bool
writer IDataFileWriter The writer to write to
dt System.Data.DataTable The datatable to write
return bool
        public static bool WriteDataTable(IDataFileWriter writer, DataTable dt)
        {
            try
            {
                int colCount = dt.Columns.Count;
                List<string> colNames = new List<string>(colCount);
                foreach (DataColumn col in dt.Columns)
                {
                    colNames.Add(col.ColumnName);
                }

                writer.CreateTable(string.Empty, colNames);

                string[] rowValues = new string[colCount];
                foreach (DataRow row in dt.Rows)
                {
                    for (int i = 0; i < colCount; i++)
                    {
                        rowValues[i] = row[i] + string.Empty;
                    }

                    writer.WriteLine(rowValues);
                }
                writer.Flush();

                return true;
            }
            catch (Exception ex)
            {
                _log.Error("Error writing table to writer", ex);
            }
            return false;
        }
FileWriterHelpers