I just found a few answers for this, but found them all horribly long with lots of iterations, so I came up with my own solution:
Convert table to string:
string myTableAsString =
String.Join(Environment.NewLine, myDataTable.Rows.Cast<DataRow>().
Select(r => r.ItemArray).ToArray().
Select(x => String.Join("\t", x.Cast<string>())));
Then simply save string to text file, for example:
StreamWriter myFile = new StreamWriter("fileName.txt");
myFile.WriteLine(myFile);
myFile.Close();
Is there a shorter / better way?
If you consider XML as text you can do: myDatatable.WriteXml("mydata.xml")
and myDatatable.ReadXml("mydata.xml")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With