This is a surprisingly involved (yet straightforward) question. The collection classes they used are hard to work with. I came up with a basic implementation. It doesn't attempt to line up row values in the correct column, but the output is a little nicer than Juran's.
The main answer is in the ToPrettyString()
extension method. Most of the other extension methods are just there to make life easier due to shortcomings in the framework.
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var ds = new DataSet();
var customersTable = ds.Tables.Add("Customers");
customersTable.Columns.AddRange("FirstName", "LastName", "Id", "Address");
customersTable.Rows.Add("Bob", "Sagget", 1, "123 Mockingbird Lane");
customersTable.Rows.Add("John", "Doe", 2, "1600 Pennsylvanie Ave");
customersTable.Rows.Add("Jane", "Doe", 3, "100 Main St");
Console.WriteLine(ds.ToPrettyString());
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
static class ExtensionMethods
{
public static string ToPrettyString(this DataSet ds)
{
var sb = new StringBuilder();
foreach (var table in ds.Tables.ToList())
{
sb.AppendLine("--" + table.TableName + "--");
sb.AppendLine(String.Join(" | ", table.Columns.ToList()));
foreach (DataRow row in table.Rows)
{
sb.AppendLine(String.Join(" | ", row.ItemArray));
}
sb.AppendLine();
}
return sb.ToString();
}
public static void AddRange(this DataColumnCollection collection, params string[] columns)
{
foreach (var column in columns)
{
collection.Add(column);
}
}
public static List<DataTable> ToList(this DataTableCollection collection)
{
var list = new List<DataTable>();
foreach (var table in collection)
{
list.Add((DataTable)table);
}
return list;
}
public static List<DataColumn> ToList(this DataColumnCollection collection)
{
var list = new List<DataColumn>();
foreach (var column in collection)
{
list.Add((DataColumn)column);
}
return list;
}
}
}
Output:
--Customers--
FirstName | LastName | Id | Address
Bob | Sagget | 1 | 123 Mockingbird Lane
John | Doe | 2 | 1600 Pennsylvanie Ave
Jane | Doe | 3 | 100 Main St