none
Connection. RRS feed

  • Question

  • Hello,

    Fairly new to LINQ. I have the following code in a silverlight web service:

     Dim datacontext As New cntxtCompanies
        Return ((From CompanyLocations _
          In datacontext.CompanyLocations _
          Where CompanyLocations.CompanyID = iCompanyID).ToList())
    
    
    

    Works fine...but last night I ran across a different sample that was putting the returned list into a list and first closing the context connection with something like the following:

     datacontext.Connection.Close()
    

    Is this best practice to close or is returning the list without closing standard.

    Thanks in advanced,

    Larry 

    Tuesday, August 10, 2010 5:44 PM

Answers

All replies

  • It is unnecessary (and potentially risky) to call Connection.Close().  Let the framework handle this for you when the datacontext drops out of scope, or for more explicit control (which I favor) create the datacontext in a using block so you know exactly where and when it lives.

     

    Tuesday, August 10, 2010 6:06 PM
  • Hi Larry,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 16, 2010 1:47 AM
    Moderator
  • I know this is an old question, so I hope this follow-up is OK.

    My understanding is that the framework will only close connections that it opened. 

    So, for a case where we need to explicitly open the connection, then what is the best practice for closing it?  For example:

    using(MyDataContext dc = new MyDataContext() {
    
         dc.Connection.Open();
         dc.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
    
         var results = [using linq to get data here ]
    
         dataGrid.DataSource = results;
         dataGrid.DataBind();
    
        dc.Connection.Close();
    }
    

    thanks
    Monday, December 5, 2011 3:50 PM