目前我有代码通过SQL连接查找数据库表,并将前五行插入数据表(表)。
using(SqlCommand _cmd = new SqlCommand(queryStatement, _con))
{
DataTable Table = new DataTable("TestTable");
SqlDataAdapter _dap = new SqlDataAdapter(_cmd);
_con.Open();
_dap.Fill(Table);
_con.Close();
}
如何将此表的内容打印到控制台以供用户查看?
在挖掘之后,我是否可能将内容绑定到列表视图,或者有没有办法直接打印它们?我不关心现阶段的设计,只关注数据。
任何指针都会很棒,谢谢!
答案 0 :(得分:30)
你可以试试这段代码:
foreach (DataRow dataRow in Table.Rows)
{
foreach (var item in dataRow.ItemArray)
{
Console.WriteLine(item);
}
}
更新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);
}
}
答案 1 :(得分:1)
这是另一个将表转储到逗号分隔字符串的解决方案:
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;
}
答案 2 :(得分:1)
使用列
打印数据表内容行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
答案 3 :(得分:1)
这是由包含单个表的数据表
完成的 SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("select * from info", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
Console.WriteLine(dt.Columns[0].ColumnName.ToString());
Console.WriteLine(dt.Rows[1].ItemArray[0].ToString());
这是通过保存表集
的数据集完成的 SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("select * from info", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet dt = new DataSet();
ad.Fill(dt);
Console.WriteLine(dt.Tables[0].Columns[0].ColumnName.ToString());
Console.WriteLine(dt.Tables[0].Rows[0].ItemArray[0].ToString());
两者都会给出相同的结果。只有数据集包含表的索引号。
答案 4 :(得分:1)
正确的表格显示
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();
}
}
答案 5 :(得分:0)
这里是基于另一个答案的方法,它是一种将DataTable转换为CSV输出并将列名添加为输出中第一行的方法:
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;
}
用法示例:
DataTable table = new DataTable();
....
Console.WriteLine(DataTableToCsv(table));
请注意,此方法无法正确处理(即转义)包含引号或逗号的数据。但是,这足以将数据表转储到控制台或其他任何地方进行查看。
答案 6 :(得分:0)
使用以下扩展方法。
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();
}
答案 7 :(得分:0)
[select * from sales where user_id=$logged_in_user_id order by sale_id desc limit 0,1]
1 以前的答案与此类似,但没有完全格式化行条目和单元格之间的分隔
答案 8 :(得分:0)
同时打印 DataTable 和 DataRow 的函数。
private void printDataTable(DataTable Table)
{
foreach (DataRow dataRow in Table.Rows)
{
printDataRow(dataRow);
}
}
private void printDataRow(DataRow drow)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Join(",", drow.ItemArray));
Console.WriteLine(sb);
}