DataContext Declaring Dim vs. Using

Hi EE,

I am using Linq in my web application.  After reading a lot of different articles on Linq, I am confused on whether I should just Dim my declaration of the datacontext or wrap code in a Using statement.  
Which one is best and why?

Dim dc as new mydatacontext


Using dc as new mydatacontext

End Using

Open in new window

Who is Participating?
Imran Javed ZiaConnect With a Mentor Consultant Software Engineer - .NET ArchitectCommented:

if you don't have to use your code/data item outside the block then best will be to use using as it will cause the system release the resources as soon as possible. so using is better for performance and resource management

on the other hand if you have something to be populated in on method and used in other then you have to stick with dim

CodeCruiserConnect With a Mentor Commented:
The DataContext does all the change tracking. If you use Dim, the context will track all changes made to the objects retrieved through that context. It will also provide the lazy loading of child objects. If you use using, obviously changes will not be tracked and you would not be able to do dbcontext.submitchanges.

This is how it is in winforms not sure if it tracks changes across postbacks in

Also, if you do not need to track changes or lazy load then using is preferred.
SheritlwAuthor Commented:
Great!  Thanks so much.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.