?
Solved

ADO.NET, data adapter, datatable, commandbuilder questions

Posted on 2006-06-15
3
Medium Priority
?
807 Views
Last Modified: 2006-11-18
Hi all,

I currently use the following approach to using the ADO.NET framework for accessing my access database:

 sSQL = "SELECT Statement"

dt = new datatable
da = New OleDb.OleDbDataAdapter
cbCommandBuilder = New OleDb.OleDbCommandBuilder
da.Update(dt)

My question is when I want to destroy the dt datatable do I have to also destroy the commandbuilder object and the data adapter object?   I expect that all these object will be destroyed also when they go out of scope.

Also, I was wondering if I destroy object dt (datatable) and then recreate it.  Do I end up with existing data from the previous object?  Will I need to clear it?

Also If I destroy the dt object but not the da or cbCommandBuilder objects....and then I create a new dt object will the existing link to the commandbuilder object work with the new instance of the dt object or would I have to create a new data adapter and command builder object for the newly created datatable?

I hope these questions make sence.

Thanks




0
Comment
Question by:mike_allain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 16916612
When you destroy your datatable you do not have to destroy the dataadapter and/or the commandbuilder.  Any object, including a dataadapter and a commandbuilder will be destroyed when it goes out of scope.  But, for instance, if you give your dataadpater a form wide scope and then apply the commandbuilder to it within a more limited scope, the dataadapter will while it (not the commandbuilder) remains in scope retain the Insert, Delete and Update commands that the commandbuilder "gave it".

If a datatable is destroyed - either explicitly by dt=Nothing, or implicitly by going out of scope - it will not only lose its data it will also lose its structure: it is not an object at all any longer.  If the datatable is not destroyed, you can make it lose its data, but retain its structure, with .Clear().

There is no permanent link between a dataadapter and a datatable.  So, if you destroy a datatable you can then use the same dataadapter to (create and) fill another datatable.  I say "(create and) fill" because, while there must be an existing datatable for the dataadpater's .Fill method - which you don't mention in your code - to operate on, that .Fill method will, if necessary, give any new datatable the structure that its SelectCommand requires.

I hope, if your question did make the sense I think it did, the above gives sensible answers ;-)

Roger
0
 

Author Comment

by:mike_allain
ID: 16926653
Thanks Roger

Its what I was looking for.... one quick question however I noticed you used dt=nothing.  Is this still the proper way od destroying an object or should I use dt.Dispose()

Mike
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16926742
Mike

Garbage collection is not my strong point ;-)

For general purposes, I don't think it matters which you use.  I say "for general purposes" because there can be occasions when you specifically want an object's "dispose" sub to fire - see this

   http://msdn2.microsoft.com/en-us/library/2z08e49e.aspx

By and large, though, whether you Dispose, or set to Nothing, or simply let something go completely out of scope, my understanding is that the GarbageCollector will eventually tidy it away and reclaim its resources.  It will do this sooner if resources are getting short, or just when it gets round to it if there is no pressure on resources.  I let it get on with it, using .Dispose or Nothing as the mood takes me with no real rhyme or reason, and have never encountered a problem.  But, as I say, I'm not an expert in relation to it and it may be that one way is "better" than the other.

Roger
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question