Print Contents Of A DataTable
you can try this code :
foreach(DataRow dataRow in Table.Rows)
{
foreach(var item in dataRow.ItemArray)
{
Console.WriteLine(item);
}
}
Update 1
DataTable Table = new DataTable("TestTable");
using(SqlCommand _cmd = new SqlCommand(queryStatement, _con))
{
SqlDataAdapter _dap = new SqlDataAdapter(_cmd);
_con.Open();
_dap.Fill(Table);
_con.Close();
}
Console.WriteLine(Table.Rows.Count);
foreach(DataRow dataRow in Table.Rows)
{
foreach(var item in dataRow.ItemArray)
{
Console.WriteLine(item);
}
}
Here is another solution which dumps the table to a comma separated string:
using System.Data;
public static string DumpDataTable(DataTable table)
{
string data = string.Empty;
StringBuilder sb = new StringBuilder();
if (null != table && null != table.Rows)
{
foreach (DataRow dataRow in table.Rows)
{
foreach (var item in dataRow.ItemArray)
{
sb.Append(item);
sb.Append(',');
}
sb.AppendLine();
}
data = sb.ToString();
}
return data;
}