I’m just writing a test application to work out how I can optimise searching on a large number of records, to be displayed in a gridview.
For the time being, I have a test table with 20,000 records (non-indexed at the moment) and I’m looking to display the records in batches of 20 in my gridview. I’m not really sure how to do this though and I’m trying to understand the theory behind it.
namespace TestingIndexes
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnStart_Click(object sender, EventArgs e)
{
gvResults.DataSource = GetContacts();
gvResults.DataBind();
}
private List<Contact> GetContacts()
{
List<Contact> contacts = new List<Contact>();
DataClasses1DataContext dc = new DataClasses1DataContext();
// Track time elapsed
Stopwatch sw = new Stopwatch();
sw.Start();
var result = from c in dc.GetTable<Contact>()
select c;
sw.Stop();
ltMessage.Text = "<p>Time elapsed: " + sw.ElapsedMilliseconds + "</p>";
if (result.Count() > 0)
contacts = result.ToList<Contact>();
return contacts;
}
}
}
As you can see, this is a very simple query using LINQ and I’m pretty sure I don’t want to be bringing 20,000 records into memory every time in order to bind it to the gridview. What is the alternative though? I suppose what I’m looking to do is ultimately query the database to find out many records there are, but then only show 20 records at a time in the gridview. Is this something that is handled by SQL Server? I’m sure there must be articles on what I’m trying to do but I’m not entirely sure what I should be searching for. Any pointers or links to tutorials would be very helpful!
Can anyone help point me in the right direction?
You should use
SkipandTaketo get parts of data from a database table.