The using statement guarantees that resources are properly disposed of after their use. This eliminates the need to remember to close database connections and will dispose of objects wrapped in the statement as soon as they leave scope. Even though the .NET Framework has built in memory management, there is still time between when the objects are no longer needed and when their unneeded memory is released to the rest of the program.
The following is an example of data access using the using statement:
using(_ctx = new SampleDBDataContext()){
var mainQuery = (from c in _ctx.GetTable<SampleTable>()
where c.isMain == true select c);
...
}
The using statement defines the scope of _ctx. So as soon as the database operations are complete, _ctx is trashed.Here is a more in-depth writeup of using using with data access - http://www.w3enterprises.com/articles/using.aspx
MSDN Reference Entry - http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx
Hey didn't I show you this?
ReplyDeleteKudos to Tim for introducing me to the using statement.
ReplyDelete