2004/06/18追記
解決策をhttp://blogs.users.gr.jp/naka/archive/2004/06/18/3218.aspxこちらに記載。
DataAdapterってComponentModel.Componentを継承しているんですよねー
これって確かにデザイナで置けるようにするためなんだろうけど必要以上にDisposeを要求するという負の面もあるわけです。
で利用したら処分しなくちゃいけないわけで、デザイナ上で利用しない場合には
public static void Disposer(OleDbDataAdapter da)
{
if ( da != null )
{
if ( da.SelectCommand != null )
{
if ( da.SelectCommand.Connection != null )
{
da.SelectCommand.Connection.Close();
da.SelectCommand.Connection.Dispose();
da.SelectCommand.Connection = null;
}
da.SelectCommand.Dispose();
da.SelectCommand = null;
}
if ( da.InsertCommand != null )
{
if ( da.InsertCommand.Connection != null )
{
da.InsertCommand.Connection.Close();
da.InsertCommand.Connection.Dispose();
da.InsertCommand.Connection = null;
}
da.InsertCommand.Dispose();
da.InsertCommand = null;
}
if ( da.UpdateCommand != null )
{
if ( da.UpdateCommand.Connection != null )
{
da.UpdateCommand.Connection.Close();
da.UpdateCommand.Connection.Dispose();
da.UpdateCommand.Connection = null;
}
da.UpdateCommand.Dispose();
da.UpdateCommand = null;
}
if ( da.DeleteCommand != null )
{
if ( da.DeleteCommand.Connection != null )
{
da.DeleteCommand.Connection.Close();
da.DeleteCommand.Connection.Dispose();
da.DeleteCommand.Connection = null;
}
da.DeleteCommand.Dispose();
da.DeleteCommand = null;
}
da.Dispose();
}
}
ちょっと冗長だけどこんだけ必要なのよね。
Disposeを誰がするのか、内部に持っているインスタンスのDisposeを誰がするのかっていうのもかなり難しい問題で、DataAdapterが勝手にDisposeしてもいいとは思うんだけど、もしかしてConnectionは他でも使っているかもしれない。
難しいね