How to Disconnect from a Database Safely

After database connection and access are complete, applications must disconnect from the database to properly release un-managed resources. These un-managed resources are provided to DG by the Common Language Runtime (CLR) for network access. In general, DG and the CLR will not release these un-managed resources for you, and if not released, unexpected results, including loss of data, can occur. You must explicitly release un-managed resources associated with AdgConnection objects using the AdgConnection.Close or AdgConnection.Dispose methods. You should call one of these methods before the AdgConnection variable loses scope or is reassigned.

  Cx.Close();
  Cx = null;

Whether you use AdgConnection.Close or AdgConnection.Dispose is a matter of preference since they perform the same task of disconnecting the database and releasing un-managed resources. Note that AdgConnection.Dispose implements the conventional System.IDisposable interface for AdgConnection.

See Also

AdgConnection Class
AdgConnection.Close Method
AdgConnection.Dispose Method
Connecting to a Database