I saw some related posts, but I want to double check something.
I have a factory class that returns DataAccessAdapter:
public static DataAccessAdapter GetDataAdapterForUpdate(string connString)
{
DataAccessAdapter a = new DataAccessAdapter(connString);
a.KeepConnectionOpen = true;
a.OpenConnection();
//Do some other stuff here that requires connection to be open
return a;
}
Since I manually called OpenConnection, do I need to explicitely call CloseConnection or is it sufficient to wrap adapter with using() scope? Below, is example 1 safe/sufficient or do I need to go with example 2?
Example 1:
using (DataAccessAdapter a = MyDataAdapterFactory.GetDataAdapterForUpdate())
{
//use adapter here
}
Example 2:
using (DataAccessAdapter a = MyDataAdapterFactory.GetDataAdapterForUpdate())
{
try { }
finally { a.CloseConnection(); }
}
Thank you.