This is the first time I have done any sort of work with flat files. I need this to be a plain txt file NOT XML.
I have written the following opting for a comma delimited format.
public static void DataTableToFile(string fileLoc, DataTable dt)
{
StringBuilder str = new StringBuilder();
// get the column headers
foreach (DataColumn c in dt.Columns)
{
str.Append(c.ColumnName.ToString() + ",");
}
str.Remove(str.Length-1, 1);
str.AppendLine();
// write the data here
foreach (DataRow dr in dt.Rows)
{
foreach (var field in dr.ItemArray)
{
str.Append(field.ToString() + ",");
}
str.Remove(str.Length-1, 1);
str.AppendLine();
}
try
{
Write(fileLoc, str.ToString());
}
catch (Exception ex)
{
//ToDO:Add error logging
}
}
My question is: Can i do this better or faster?
And str.Remove(str.Length-1, 1);
is there to remove the last ,
which is the only way I could think of.
Any suggestions?