IDisposable, or how to write bad sample code
I've started my new job and am working on some data access code to IBM's DB2. I came across the following two incongruous pages in the IBM Data Server Provider for .NET reference.
First, there's the DB2Connection class. In the remarks, it specifically states:
However, on the DB2DataAdapter class reference page, they happily ignore their own advice. They also fail to mention that DB2DataAdapter is disposable and should be handled as such.
Here's my recommendation for new sample code:
First, there's the DB2Connection class. In the remarks, it specifically states:
TheDB2Connection
object uses native resources. You should always explicitly close any openDB2Connection
objects by calling Close or Dispose before theDB2Connection
object goes out of scope
However, on the DB2DataAdapter class reference page, they happily ignore their own advice. They also fail to mention that DB2DataAdapter is disposable and should be handled as such.
Here's my recommendation for new sample code:
public DataSet SelectDB2SrvRows(DataSet dataset,string connection,string query)
{
using (DB2Connection conn = new DB2Connection(connection))
{
using (DB2DataAdapter adapter = new DB2DataAdapter())
{
adapter.SelectCommand = new DB2Command(query, conn);
adapter.Fill(dataset);
}
}
return dataset;
}
Comments
Post a Comment