Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was curious, I was trying to redo some of my code and found out by accident that if you choose to use an SQL adaptor you don't need an open and close statement, but why? whats the difference? Also which is better to use?

What I have tried:

No problems, just curious because I can change some of my code based upon the answers.
Posted
Updated 27-Jul-17 9:24am

Because, internally, the DbDataAdapter classes open and close the connection themselves.
 
Share this answer
 
Comments
Member 11856456 27-Jul-17 13:22pm    
So it would be best to use an adaptor over a regular statement? i didn't know that the connections would open and close by themselves. What about previous data accessed, is it disposed of before the new data is accessed?
Dave Kreskowiak 27-Jul-17 13:51pm    
It does not Dispose anything for you. You still have to do that yourself.

A DataAdapter is good for maintaining a DataTable object if you need to use that, but it has penalties for using it due to its "generic for all situations" nature and implementation.

If you're handing managing all the data and updates yourself (I always do), then you don't need a DataAdapter.

If you're handling large sets of data where the data is too large to fit in memory all at once, you cannot use a DataSet, DataTable, and DataAdapter.
You might find reference source interesting. The filling is done in class DbDataAdapter in method called FillInternal.

If you investigate the code you'll see that the connection is opened and closed unless it's already open. If the connection is initially in open state, it's left that way.

Have a look at Reference Source - DbDataAdapter[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900