Click here to Skip to main content
16,005,058 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am seeing some very wierd Linq behaviour in my application and was wondering if anyone could explain it. I'm using .Net 3.5 and MSSQL Server 2008

The first time I run the method, the DB connection remains closed. The second time I run it the connection opens and remains open.

Any ideas why?

XML
public List<sp_MySPResult> GetData(Int32 customerID) {
   var result = new List<sp_MYSPResult>();
   try{
      result = (from n in DB.sp_MYSP(customerID)
                select new sp_MYSPResult{
                    Name = n.Name,
                     Surname = n.Surname}
               ).ToList();
   } catch (Exception ex){
      Console.WriteLine(ex.Message);
   }
   return result;
}
Posted

1 solution

Perhaps this function is not closing the connection properly
C#
sp_MYSP


There is a chance that on one of the records it fails and throws an exception so then it causes call to end prematurely and therefore not calling the close on the database.
It's always a good idea to have the close for a stream or database connection in a final scope. That way if it fails, the connection / stream is always closed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900