Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ADO.NET, data adapter, datatable, commandbuilder questions

Posted on 2006-06-15
3
Medium Priority
?
809 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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