2016-03-20 56 views
1

我有100000条记录的数据表,我想通过数据表迭代我想保存记录的每10,000条记录。对于下一次迭代,我要保存下一个10000条记录,直到100000条记录。如何迭代数据表

DataTable dt = new DataTable(); 

dt = ds.tables[0]; //here i am getting 100,000 records 

for (int i = 0; i < dt.rows.count; i + 10000) 
{ 
    savedatatable(dt[i]); 
} 

回答

1

应该是这样的:

for (int i = 0; i < dt.Rows.Count; i+=10000) 
{ 
    DataRow dr = dt.Rows[i]; 
    // do something 
} 
+0

第一条评论是不必要的,因为你已经用'i J3soon

+0

@ J3soon,只要您确定您的行数为%10000 = 0,否则您的索引可能超出范围。 – AsfK

+0

由于'i + = 10000'在'body'之后被调用,因此可以将'i J3soon

0

你应该使用下面的代码:

DataTable dt = new DataTable(); 

dt = ds.tables[0]; //here i am getting 100,000 records 

//Loop through columns in rows 
for (int i = 0; i < dt.rows.count && i < 100000; i += 10000) 
{ 
    foreach (DataColumn col in dt.Columns) 
     savedatatable(dt.Rows[col.ColumnName].ToString()); 
} 

DataTable dt = new DataTable(); 

dt = ds.tables[0]; //here i am getting 100,000 records 

//Loop through rows in columns 
foreach (DataColumn col in dt.Columns) 
{ 
    for (int i = 0; i < dt.rows.count && i < 100000; i += 10000) 
     savedatatable(dt.Rows[col.ColumnName].ToString()); 
} 

这里有一个类似的问题,但我米不知道这是你想要的。 :Looping through a DataTable