We have a utility SQL class that takes the name of the stored procedure and its input parameters, and returns the results in a datatable. The rationale for this is that we donβt have to worry about forgetting about closing the connections and leaking the connections. In addition, we can reduce code without backing up data and datareaders in our data access layers.
The problem with this is that we are populating a datatable so that we can scroll through it to create our objects, so we mainly use it as a datareader. I read about classes that will return a datareader or dataadapter. But the problem is that either the client must open and close the connections, or close the connection in the Finalize method. It seems that you do not want garbage collection to be responsible for closing database connections.
To summarize, we want to have a class so that we can shorten the code without creating datareaders for each query and so that we can ensure that database connections are closed.
What is the best way to handle this?
UPDATE:. Still thinking about it, but for now it seems like the best thing is still to return the datareader, use CommandBehavior.CloseConnection, and then trust the one who ever used the class to call dr. Close ()?
source share