【问题标题】:Loop Through All DataTable in DataSet Using A For Loop使用 For 循环遍历 DataSet 中的所有 DataTable
【发布时间】:2015-12-26 16:36:00
【问题描述】:

任何人都知道如何使用 For Loop 而不是 foreach 循环遍历 Dataset 中的所有数据表?

我知道 foreach 循环可以做到这一点,但 我想改用 For 循环。

例如:

foreach (DataTable table in ds.Tables)
{

}   

我想改用 For 循环。

如果有人可以帮助我,不胜感激

【问题讨论】:

  • 我想知道为什么DataTableCollection(从DataSet.Tables 返回)没有实现IList,而只有ICollection,它没有随机访问但只有Count 属性。这是一个indexer property,它允许通过索引访问表。

标签: c# for-loop foreach datatable dataset


【解决方案1】:

您可以使用ds.Tables.Count 属性来执行此操作:

 for (int i = 0; i < ds.Tables.Count; i++)
 {
     // access your table with indexes:
     Console.WriteLine(ds.Tables[i].ToString());
 }

【讨论】:

    【解决方案2】:
    DataSet  dt = new DataSet();
    //Populate dataset here
    //Iterate throuh datatables inside the dataset
    for(int i=0;i<dt.Tables.Count;i++)
        {
          DataTable temptable = dt.Tables[i]; // this will give you the datatable in each iteration level
            //Do your doce here
        }
    

    【讨论】:

      【解决方案3】:
      DataSet dsTemp = new DataSet();
      for (int tableIndex = 0; tableIndex < dsTemp.Tables.Count; tableIndex++)
      {
          DataTable dtIndex = dsTemp.Tables[tableIndex];
          //code here
      }
      

      这样……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多