Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close DataSet's underlying connection explicitly?

Tags:

c#

ado.net

I am using DataSet to retrieve data from the Microsoft SQL Server. Do I need to explicitly close the connection (or the underlying SqlDataAdapter automatically closes the connection)?

I always use DataReader (with using), but first time using DataSet -- that's why wondering about best practice. Thanks in advance.

like image 672
Sha Le Avatar asked Oct 14 '22 03:10

Sha Le


1 Answers

A DataSet is a disconnected 'view' on the database. That is, you load the data from the database in a DataSet (actually, in a DataTable, which can be put in a DataSet), and you can close the Connection that you've used to populate the DataTable or DataSet.

You can continue to work with the data that is in the dataset. It does not require an open connection to the DB.

In fact, you should close a DB-connection as soon as you don't need any DB access soon. Connections to databases should be short-lived.

like image 72
Frederik Gheysels Avatar answered Oct 18 '22 15:10

Frederik Gheysels