Domanda

I know there is probably an obvious reason but I can't find it..

I usually use the using statement during DB connection and data reading, but I can't use it on DataAdapter because it doesn't implement IDisposable.

Also: how can it behave after filling a dataset?

Does it close itself the connection?

Only close it or dispose it?

Do I have to dispose the internal connection by myself after the dataset filling? If so, why IDataAdapter doesn't have a related property/method?

È stato utile?

Soluzione 3

DataAdapter dosn't implement IDisposable because it doesn't have any member variables that need to be handled outside of normal garbage collection. In other words it doesn't need to implement IDisposable.

If your intrested you can see the source code to the Mono DataAdapter here : http://www.java2v.com/Open-Source/CSharp/2.6.4-mono-.net-core/System.Data/System/Data/Common/DataAdapter.cs.htm

In general if you going to close the connection after using it, go ahead in put the connection in a using scope, but its ok too to have a singleton and have multipal DataAdapters in multipal scopes using the same connection.

Hope this makes sense.

Altri suggerimenti

A DataAdapter instance does not hold references to unmanaged code. It simply acts as a "bridge" between the commands and the datasets.

The DataAdapter serves as a bridge between a DataSet and a data source for retrieving and saving data. The DataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source, and Update, which changes the data in the data source to match the data in the DataSet.

Source: http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.aspx

DataAdapter doesn't close the connection as you might want to keep using this connection to populate other DataSets.

The exception is when the connection wasn't in open state before Fill method was called (Thanx Richard): https://stackoverflow.com/a/2472886/2258

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top