打印数据表的内容 [英] Print Contents Of A DataTable

查看:60
本文介绍了打印数据表的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有代码通过SQL连接查找数据库表,并将前五行插入数据表(表)中。

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 = new SqlCommand(queryStatement, _con))
{
    DataTable Table = new DataTable("TestTable");

    SqlDataAdapter _dap = new SqlDataAdapter(_cmd);

    _con.Open();
    _dap.Fill(Table);
    _con.Close();
}

然后我如何将该表的内容打印到用户的控制台上

How do I then print the contents of this table to the console for the user to see?

挖掘之后,是否有可能将内容绑定到列表视图,或者是否可以直接打印它们?

After digging around, is it possible that I should bind the contents to a list view, or is there a way to print them directly? I'm not concerned with design at this stage, just the data.

任何指针都很好,谢谢!

Any pointers would be great, thanks!

推荐答案

您可以尝试以下代码:

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);
    }
}

这篇关于打印数据表的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆