Fix resource leak and improve performance of DataProvider.SaveToFile().