2013-10-15 17 views
0

我應該如何清除我在C#代碼(Windows Mobile Compact Framework)中每次新SQL調用時分配給new Dataset()的DataSet?Clear()或Dispose()清除分配給C#中新數據集()的數據集?

目前我使用Clear(),但是我不確定是否應該使用Dispose。什麼是適當的選擇呢?

一些代碼:

內frmA(主要形式,我執行SQL搜索數據庫)保存我的搜索結果到數據庫中,如果計數大於0,我去爲另一種形式frmNewWork做了一些工作,發現數據:

// search SQL 
// declare connectionString and command... 
SqlCeDataAdapter adapter = new SqlCeDataAdapter(mCommand); 
modFunctions.tempDataset = new NEWDataSet(); 
adapter.Fill(modFunctions.tempDataset, "item"); 
foundCount = modFunctions.tempDataset.item.Count; 

// Goes off to another form to do somework with that dataset 
if (foundCount > 0) 
{ 
    frmNewWork myForm = new frmNewWork(); 
    myForm.ShowDialog() 
} 

現在...當我從myForm回來,我想明確的是,數據庫,我這樣做,在內部形成GotFocus功能:

private void frmA_GotFocus(object sender, EventArgs e) 
{ 
    // Clear prior tempDataset (was .Clear() originally) 
    modFunctions.tempDataset.Dispose(); 
} 
+0

郵政編碼,給上下文。 Btw Clear(),Dispose()和新的DataSet()服務於不同的目的 –

+0

對不起,我添加了一些代碼。 – 22332112

回答

2

兩點

1)如果要在此之後使用的ShowDialog()方法示出的其它形式來清除數據集。您可以在您的myForm.ShowDialog();聲明後執行此操作。如果ASTSDataSet是非託管呼叫處置。否則,不要做任何事情,GC會照顧。

+1

如果它實現了'IDisposable',總是調用'Dispose'。 – Gene

相關問題