使用For循环遍历DataSet中的所有DataTable

时间:2015-09-29 08:58:40

标签: c# for-loop foreach datatable dataset

任何人都知道如何使用For Loop代替foreach循环遍历数据集中的所有数据表?

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

例如:

foreach (DataTable table in ds.Tables)
{

}   

我想改用For循环。

感谢有人可以帮助我

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分: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
    }

答案 2 :(得分:2)

DataSet dsTemp = new DataSet();
for (int tableIndex = 0; tableIndex < dsTemp.Tables.Count; tableIndex++)
{
    DataTable dtIndex = dsTemp.Tables[tableIndex];
    //code here
}

通过这种方式......