C#,循环遍历数据集并显示数据集列中的每条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15252223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:41:38  来源:igfitidea点击:

C#, Looping through dataset and show each record from a dataset column

c#foreachdataset

提问by Peter

In C#, I'm trying to loop through my dataset to show data from each row from a specific column. I want the get each date under the column name "TaskStart" and display it on a report, but its just shows the date from the first row for all rows can anybody help?

在 C# 中,我试图遍历我的数据集以显示特定列中每一行的数据。我希望在列名“TaskStart”下获取每个日期并将其显示在报告中,但它只显示所有行的第一行的日期有人可以帮忙吗?

 foreach (DataTable table in ds.Tables)
 {

     foreach (DataRow dr in table.Rows)
     {
         DateTime TaskStart = DateTime.Parse(
             ds.Tables[0].Rows[0]["TaskStart"].ToString());
         TaskStart.ToString("dd-MMMM-yyyy");
         rpt.SetParameterValue("TaskStartDate", TaskStart);
     }
 }

采纳答案by Denys Denysenko

DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());

回答by bash.d

I believe you intended it more this way:

我相信你更倾向于这样:

foreach (DataTable table in ds.Tables)
{
    foreach (DataRow dr in table.Rows)
    {
        DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
        TaskStart.ToString("dd-MMMM-yyyy");
        rpt.SetParameterValue("TaskStartDate", TaskStart);
    }
}

You always accessed your first row in your dataset.

您总是访问数据集中的第一行。

回答by Ch?a bi?t

foreach (DataRow dr in ds.Tables[0].Rows)
{
    //your code here
}

回答by Vaishakh

foreach (DataTable table in ds.Tables)
{
    foreach (DataRow dr in table.Rows)
    {
        var ParentId=dr["ParentId"].ToString();
    }
}