Pull records from the database one batch at a time
Hi,
I am trying to learn how to use Iterators to pull records from a database one batch at a time in a clean and reusable manner but am strugling to find any tutorials online.
I have created a simple databse with 15000 Blog Post records but do not want to bring all those records at the same time.
The problems is that I am not sure how to create the SQL statement to be able to batch groups of statements together.
How can we get a batch of records from the Database and then when we go back for the second batch how does it know what record (database row) to start from?
This is how I would simulate loading posts from the database. But this is hard coded and not from the database
static ICollection<BlogPost> GetPostBatch(int index)
{
List<BlogPost> posts = new List<BlogPost>();
// code to simulate loading posts from the database...
switch (index)
{
case 0:
posts.AddRange(new BlogPost[]
{
new BlogPost("First Post")
, new BlogPost("Second Post")
, new BlogPost("Third Post")
});
break;
case 1:
posts.AddRange(new BlogPost[]
{
new BlogPost("Fourth Post")
, new BlogPost("Fifth Post")
, new BlogPost("Sixth Post")
});
break;
case 2:
posts.AddRange(new BlogPost[]
{
new BlogPost("Seveth Post")
, new BlogPost("Eighth Post")
});
break;
case 3:
//no more data to return.
return null;
}
return posts;
}
I would appreciate if someone could point me to an online tutorial or provide some sample code.
Cheers
CP
|