• 0

[ASP.NET] I have a function that returns a dataset


Question

Hello,

So I have a function that returns a DataSet. When do I close the databse connection? Can I close it before returning the DataSet?

Example:

	public static DataSet Addresses_for_a_Client_by_ID(string cID)
	{
		/* snipped */

		SqlDataAdapter da = new SqlDataAdapter();
		da.SelectCommand = cmd;
		DataSet ds = new DataSet();
		da.Fill(ds, "ClientAddresses");

		// Can I do: connection.Close(); here? 
		return ds;
	}

4 answers to this question

Recommended Posts

  • 0

Yep. You can close the connection before returning the dataset. Ideally you would want to do "da.Dispose()" between "da.Fill(...)" and "return ds;" The dataset holds the results of the query in its own data structure, so once you've got the data, you don't need to be connected to the database anymore.

  • 0
  Majesticmerc said:
Yep. You can close the connection before returning the dataset. Ideally you would want to do "da.Dispose()" between "da.Fill(...)" and "return ds;" The dataset holds the results of the query in its own data structure, so once you've got the data, you don't need to be connected to the database anymore.

DataSet.Dispose doesn't really do anything, its an inherited member from a lower level of the abstraction tree.

  • 0
  Antaris said:
DataSet.Dispose doesn't really do anything, its an inherited member from a lower level of the abstraction tree.

Ah fair enough. Cheers :)

Edited by Majesticmerc
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.