I have a list of strings that are just invoice numbers. I am enumerating through this list to get the details of each invoice from the database. This list can easily be 700 to 1000 in size. the way I am doing it now results in 700-1000 connections to the database. this is taking too long to complete is there a better way to do this that I just don't know about? Any pointers would be great.
here is an example of my enumeration
foreach(string i in invoiceList)
{
Invoice inv = invoiceData.GetInvoice(i);
//do something with the invoice
}
then here is an example of my data access method using ado.net
public Invoice GetInvoice(string invoice)
{
SqlConnection con = new SqlConnection(//connection string);
SqlCommand cmd = new SqlCommand("dbo.getInvoices", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("invoice", SqlDbType.VarChar).Value = invoice;
SqlDataReader dr;
Invoice inv = new Invoice();
try{
con.Open();
dr = cmd.ExecuteReader
while(dr.read())
{
//assign values from the database fields
}
}
catch{}
finally{con.close();}
}
so basically the getInvoice method gets called 1000 times opening a new connection every time. What is a better(faster) way to do this. Thank you!