ObjectResult<T>.Dispose 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
執行與釋放、釋放或重置資源相關的任務。
public:
override void Dispose();
public override void Dispose();
override this.Dispose : unit -> unit
Public Overrides Sub Dispose ()
範例
此範例回傳 方法中的 Execute 。ObjectResult<T> 接著它會用列舉器,並遍歷查詢結果。 最後,它釋放了列舉器和 ObjectResult<T> 物件。
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
ObjectSet<Product> query = context.Products;
ObjectResult<Product> queryResults = null;
System.Collections.IEnumerator enumerator = null;
try
{
queryResults = query.Execute(MergeOption.AppendOnly);
// Get the enumerator.
enumerator = ((System.Collections.IEnumerable)queryResults).GetEnumerator();
// Iterate through the query results.
while (enumerator.MoveNext())
{
Product product = (Product)enumerator.Current;
Console.WriteLine("{0}", product.Name);
}
// Dispose the enumerator
((IDisposable)enumerator).Dispose();
}
finally
{
// Dispose the query results and the enumerator.
if (queryResults != null)
{
queryResults.Dispose();
}
if (enumerator != null)
{
((IDisposable)enumerator).Dispose();
}
}
}
備註
關閉 DbDataReader包含查詢結果的 。