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:

The DB2Connection object uses native resources. You should always explicitly close any open DB2Connection objects by calling Close or Dispose before the DB2Connection 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

Popular posts from this blog

Managing to Lead

Process Consensus

Cert-ifiably IISane