Something I was musing over while writing some code. It's not important in the grand scheme, just would help me strip out a single line of code from a few apps ^_^
I have an openDBConnection() function I can call on, creates an SqlConnection, populates the object with my connection string etc. If I open the connection and return that in the function, I can use it fine within the main body of code, that works without a problem and is what I'd ideally want to do. However, it seems to me there is the possibility of an open connection to my DB remaining within the scope of the function. This is absolutely not acceptable for obvious reasons! I would think that the c# garbage collection would tidy this up (close the connection, free up the object) as soon as the function returns? How much can I rely on it? Would it be better simply to open the connection in the main body of code? Is there a better way of going about this?
Comments appreciated ^_^ thanks in advance!
I have an openDBConnection() function I can call on, creates an SqlConnection, populates the object with my connection string etc. If I open the connection and return that in the function, I can use it fine within the main body of code, that works without a problem and is what I'd ideally want to do. However, it seems to me there is the possibility of an open connection to my DB remaining within the scope of the function. This is absolutely not acceptable for obvious reasons! I would think that the c# garbage collection would tidy this up (close the connection, free up the object) as soon as the function returns? How much can I rely on it? Would it be better simply to open the connection in the main body of code? Is there a better way of going about this?
Comments appreciated ^_^ thanks in advance!