Seems, your logic is wrong... Take a look at your code, especially at underlined parts:
dtArr() = dt.Select("key_id <= 1 And key_id >= 30000").Clone()
dtArr() = dt.Select("key_id <= 31000 And key_id >= 60000").Clone()
dtArr() = dt.Select("key_id <= 61000 And key_id >= 90000").Clone()
dtArr() = dt.Select("key_id <= 91000 And key_id >= 120000").Clone()
dtArr() = dt.Select("key_id <= 121000 And key_id >= 150000").Clone()
You mixed
less than
and
greater than
operators! Another issue is that you lose 1000 records (31000-3000=1000) between each
select
!
If you would like to split data into parts with 30000 records, you can use Linq. Take a look at example...
Method #1 - copying data by key_id
:
DataTable src = new DataTable();
src.Columns.Add(new DataColumn("key_id", typeof(int)));
src = Enumerable.Range(1,100).Select(x=>src.LoadDataRow(new object[]{x}, false)).CopyToDataTable();
int increasement = 30;
int curVal = 1;
DataRow[] result = null;
DataSet ds = new DataSet();
do
{
result = src.AsEnumerable()
.Where(x=>x.Field<int>("key_id")>=curVal && x.Field<int>("key_id")<curVal+increasement)
.ToArray();
if(result.Length>0)
{
DataTable tmp = src.Clone();
tmp = result.CopyToDataTable();
ds.Tables.Add(tmp);
}
curVal+=increasement;
} while(result.Length>0);
Dim src As DataTable = New DataTable()
src.Columns.Add(New DataColumn("key_id", Type.GetType("System.Int32")))
src = Enumerable.Range(1,100).Select(Function(x) src.LoadDataRow(New Object(){x}, False)).CopyToDataTable()
Dim increasement As Integer = 30
Dim curVal As Integer = 1
Dim result As DataRow()
Dim ds As DataSet = New DataSet()
Do
result = src.AsEnumerable() _
.Where(Function(x) x.Field(Of Integer)("key_id")>=curVal And x.Field(Of Integer)("key_id")<curVal+increasement) _
.ToArray()
If result.Length>0 Then
Dim tmp As DataTable = src.Clone()
tmp = result.CopyToDataTable()
ds.Tables.Add(tmp)
End If
curVal+=increasement
Loop While(result.Length>0)
Method #2 - copying the same number of rows:
int increasement = 30;
int curVal = 0;
DataRow[] result = null;
DataSet ds = new DataSet();
do
{
result = src.AsEnumerable()
.Skip(curVal)
.Take(increasement)
.ToArray();
if(result.Length>0)
{
DataTable tmp = src.Clone();
tmp = result.CopyToDataTable();
ds.Tables.Add(tmp);
}
curVal+=increasement;
} while(result.Length>0);
Dim increasement As Integer = 30
Dim curVal As Integer = 0
Dim result As DataRow()= Nothing
Dim ds As DataSet = New DataSet()
Do
result = src.AsEnumerable() _
.Skip(curVal) _
.Take(increasement) _
.ToArray()
If result.Length>0 Then
Dim tmp As DataTable = src.Clone()
tmp = result.CopyToDataTable()
ds.Tables.Add(tmp)
End If
curVal+=increasement
Loop While(result.Length>0)
[EDIT]
Non-Linq solution - method #1:
Dim src As DataTable = New DataTable()
src.Columns.Add(New DataColumn("key_id", Type.GetType("System.Int32")))
For i As Integer = 0 To 100
src.Rows.Add(New Object() {i})
Next
Dim increasement As Integer = 30
Dim curVal As Integer = 1
Dim result As DataRow()
Dim ds As DataSet = New DataSet()
Do
result = src.Select(String.Format("key_id >={0} And key_id<{1}", curVal, curVal + increasement)).Clone()
If result.Length > 0 Then
Dim tmp As DataTable = src.Clone()
Dim sb As StringBuilder = New StringBuilder()
For Each dr As DataRow In result
tmp.Rows.Add(New Object() {dr("key_id")})
sb.Append(String.Format("{0};", dr("key_id")))
Next
ds.Tables.Add(tmp)
Console.WriteLine("{0}", sb.ToString())
sb = Nothing
End If
curVal += increasement
Loop While (result.Length > 0)
Console.ReadKey()
Result (4 tables):
1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;
31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;
61;62;63;64;65;66;67;68;69;70;71;72;73;74;75;76;77;78;79;80;81;82;83;84;85;86;87;88;89;90;
91;92;93;94;95;96;97;98;99;100;
[END EDIT]
Good luck!