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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imran Javed ZiaConsultant 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.