DataContext Declaring Dim vs. Using

Hi EE,

I am using Linq in my asp.net 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?

Thanks
Dim dc as new mydatacontext

or

Using dc as new mydatacontext

End Using

Open in new window

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

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

Thanks
0
 
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 asp.net.

Also, if you do not need to track changes or lazy load then using is preferred.
0
 
SheritlwAuthor Commented:
Great!  Thanks so much.
0
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.