C# – How to convert a DataTable to a string in C#


I'm using Visual Studio 2005 and have a DataTable with two columns and some rows that I want to output to the console. I hoped there would be something like:

DataTable results = MyMethod.GetResults();
Console.WriteLine (results.ToString());

What's the best way (i.e. least amount of coding from me) to convert a simple DataTable to a string?

Best Solution


using System.Linq;

then ...

string res = string.Join(Environment.NewLine, 
    results.Rows.OfType<DataRow>().Select(x => string.Join(" ; ", x.ItemArray)));