开发者

How to properly return and dispose of a dataset's dataview - seeing strange results

I'm trying to figure how to correctly return a dataview. I noticed that if I bind to the resultant dataview used below, I don't get any results.

    private DataView filterDocuments(DataSet documents, string classType)
{
    using (documents)
    {
        //filter it
        using (DataView documentsByType = new DataView(documents.Tables[0], String.Format("ClassName = '{0}'", classType), String.Empty, DataViewRowState.CurrentRows))
        {
            return documentsByType;
        }
    }
}

However, if I don't dispose of my dataview, I can see 开发者_开发问答teh results I want. How can I properly return a dataview?

    private DataView filterDocuments(DataSet documents, string classType)
{
    using (documents)
    {
        //filter it
        return new DataView(documents.Tables[0], String.Format("ClassName = '{0}'",classType), String.Empty, DataViewRowState.CurrentRows);
    }
}

Do I not need to dispose of it? Am I creating a memory leak? Do I have to pass the dataset by reference?

Many thanks!


The correct place for the using (or calling DataView.Dispose() would be wherever you are making use of the DataView. It doesn't make sense to dispose of it in this spot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜