Currently I have code which looks up a database table through a SQL connection and inserts the top five rows into a Datatable (Table).
using(SqlCommand _cmd
Building on another answer here, here's a method that converts a DataTable to CSV output and adds the column names as the first row in the output:
public static string DataTableToCsv(DataTable table)
{
string result = string.Empty;
StringBuilder resultBuilder = new StringBuilder();
if (table != null && table.Rows != null && table.Columns != null && table.Columns.Count > 0)
{
int lastItemIndex = table.Columns.Count - 1;
int index = 0;
foreach (DataColumn column in table.Columns)
{
resultBuilder.Append(column.ColumnName);
if (index < lastItemIndex) // if not the last element in the row
resultBuilder.Append(", "); // add the separator
index++;
}
resultBuilder.AppendLine(); // add a CRLF after column names row
foreach (DataRow dataRow in table.Rows)
{
lastItemIndex = dataRow.ItemArray.Length - 1;
index = 0;
foreach (object item in dataRow.ItemArray)
{
resultBuilder.Append(item);
if (index < lastItemIndex) // if not the last element in the row
resultBuilder.Append(", "); // add the separator
index++;
}
resultBuilder.AppendLine(); // add a CRLF after each data row
}
result = resultBuilder.ToString();
}
return result;
}
Usage example:
DataTable table = new DataTable();
....
Console.WriteLine(DataTableToCsv(table));
Note that this method does not properly handle (i.e., escape) data that contains quotes or commas. But it should be sufficient as a quick and dirty way to dump a data table to the console or anywhere else for viewing.
Print Datatable contents rows with column
Here is solution
DataTable datatableinfo= new DataTable();
// Fill data table
//datatableinfo=fill by function or get data from database
//Print data table with rows and column
for (int j = 0; j < datatableinfo.Rows.Count; j++)
{
for (int i = 0; i < datatableinfo.Columns.Count; i++)
{
Console.Write(datatableinfo.Columns[i].ColumnName + " ");
Console.WriteLine(datatableinfo.Rows[j].ItemArray[i]);
}
}
Ouput :
ColumnName - row Value
ColumnName - row Value
ColumnName - row Value
ColumnName - row Value
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);
}
}
Use the following extension method.
public static string ToCSV(this DataTable t, bool header)
{
StringBuilder sb = new StringBuilder();
int i = 0;
int maxColIdx = 0;
if (t?.Columns?.Count > 0)
{
maxColIdx = t.Columns.Count - 1;
if (header)
{
foreach (DataColumn c in t.Columns)
{
sb.Append(c.ColumnName);
if (i < maxColIdx) sb.Append(", ");
i++;
}
sb.AppendLine();
}
if (t?.Rows?.Count > 0)
{
foreach (DataRow r in t.Rows)
{
i = 0;
foreach (var item in r.ItemArray)
{
sb.Append(item);
if( i < maxColIdx) sb.Append(',');
i++;
}
sb.AppendLine();
}
}
}
return sb.ToString();
}
Proper Tabular display
static void print_results(DataTable data)
{
Console.WriteLine();
Dictionary<string, int> colWidths = new Dictionary<string, int>();
foreach (DataColumn col in data.Columns)
{
Console.Write(col.ColumnName);
var maxLabelSize = data.Rows.OfType<DataRow>()
.Select(m => (m.Field<object>(col.ColumnName)?.ToString() ?? "").Length)
.OrderByDescending(m => m).FirstOrDefault();
colWidths.Add(col.ColumnName, maxLabelSize);
for (int i = 0; i < maxLabelSize - col.ColumnName.Length + 10; i++) Console.Write(" ");
}
Console.WriteLine();
foreach (DataRow dataRow in data.Rows)
{
for (int j = 0; j < dataRow.ItemArray.Length; j++)
{
Console.Write(dataRow.ItemArray[j]);
for (int i = 0; i < colWidths[data.Columns[j].ColumnName] - dataRow.ItemArray[j].ToString().Length + 10; i++) Console.Write(" ");
}
Console.WriteLine();
}
}
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;
}