强制调用连接对象的Dispose方法。在下面的代码中,我应该在finally块中添加conn.Dispose()吗?
static public ArrayList getData()
{
OdbcConnection conn = Connect();
ArrayList al = new ArrayList();
try
{
OdbcCommand mycmd = new OdbcCommand();
conn.Open();
mycmd.Connection = conn;
OdbcDataReader result = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
while (result.Read())
{
...
al.Add(strResult);
}
}
catch (Exception e)
{
throw new Exception(e.Message + "[]" + e.StackTrace);
}
finally
{
conn.Close();
}
return al;
}
我的看法是调用连接对象(conn.Close())的Close()方法就足够了,因为conn是托管资源,CLR会照顾它。