0

我应该使用什么来清除我new Dataset()在 C# 代码(Windows Mobile Compact Framework)中的每个新 SQL 调用上分配的 DataSet?

目前我正在使用 Clear(),但是我不确定是否应该使用 Dispose。什么是合适的选择?

一些代码:

在 frmA(我对数据库执行 SQL 搜索的主窗体)中,我将搜索结果保存到数据库中,如果 count 大于 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,我想清除该数据库,并在表单 AGotFocus函数中执行此操作:

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

2 回答 2

3

像这样写代码..

using(DataSet ds= new Dataset()
{
}

无需担心处置。

你也可以看看这个链接。 http://www.csharptuts.net/dataset-in-c/

于 2013-10-15T14:40:03.007 回答
2

两点

1) 如果您想在使用 ShowDialog() 方法显示的其他表单之后清除数据集。您可以在您的myForm.ShowDialog();声明之后执行此操作。

2)如果ASTSDataSet是非托管调用Dispose。否则什么都不做,GC会小心的。

于 2013-10-15T14:45:22.500 回答