The code is like this:
public class Email
{
public string MailAddress {get;set;}
public string MailOwner {get;set;}
public int MailSended {get;set;}
public int MailReceived {get;set;}
}
public class EmailList
{
public List<Email> Items {get; private set;}
private IDBConnection _connection;
public EmailList(IDBConnection connection)
{
_connection = connection;
}
public List<Email> GetEmails(int customerId)
{
var emailList = new List<Email>();
var sql = "SELECT * FROM EmailsAddress WHERE Id = '" + customerId + "'";
using (var ObjectToAccessDatabaseAndStoreResultsIntoDataTable = new ObjectToObtainDbResultsIntoDataTable(_connection, sql))
{
foreach(DataRow row in ObjectToAccessDatabaseAndStoreResultsIntoDataTable.Datatable)
{
var email = new Email{
MailAddress = Convert.ToString(row["MailAddress"]);
MailOwner = Convert.ToString(row["MailOwner"]);
MailSended = Convert.ToInt32(row["MailSended"]);
MailReceived = Convert.ToInt32(row["MailReceived"]);
}
emailList.Add(email);
}
return emailList;
}
}
When GetEmails()
is called multiple times to retrieve mail addresses, for i.e. 2000 customers, it takes a very long time to return. Using SQL Server Profiler, I see that most of the time is for the GetEmails()
routine.
Why is the GetEmails()
routine taking so long? I am using SQL Server.