Bazenga Posted September 10, 2009 Share Posted September 10, 2009 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; } Link to comment Share on other sites More sharing options...
0 +Majesticmerc MVC Posted September 10, 2009 MVC Share Posted September 10, 2009 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. Link to comment Share on other sites More sharing options...
0 Bazenga Posted September 10, 2009 Author Share Posted September 10, 2009 Thanks a lot. Now I have to change like 20 files ;(. I hate handling code written by someone else *sighs*. Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted September 10, 2009 Veteran Share Posted September 10, 2009 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. Link to comment Share on other sites More sharing options...
0 +Majesticmerc MVC Posted September 10, 2009 MVC Share Posted September 10, 2009 (edited) DataSet.Dispose doesn't really do anything, its an inherited member from a lower level of the abstraction tree. Ah fair enough. Cheers :) Edited September 10, 2009 by Majesticmerc Link to comment Share on other sites More sharing options...
Question
Bazenga
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; }Link to comment
Share on other sites
4 answers to this question
Recommended Posts