Print Contents Of A DataTable

后端 未结 7 1824
再見小時候
再見小時候 2020-12-05 17:35

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          


        
7条回答
  •  庸人自扰
    2020-12-05 18:00

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

提交回复
热议问题