Kit*_*nke 5 sharepoint dispose sharepoint-api
在阅读Stefan Gossner关于处理对象的帖子以及关于Cross方法处理模式的这个问题之后,我发现我犯了意外重新打开一些SPWebs的罪行.我知道在Stefan Gossner的帖子中他提到你应该在完成任何子对象之后处理SPWeb.但是,microsoft文档提到了缓存SPListItemCollection对象.以下代码是否正确?返回的SPListItemCollection会重新打开SPWeb对象吗?有什么方法可以肯定吗?
// is this correct????
private SPListItemCollection GetListItems()
{
SPListItemCollection items = null;
try
{
using (SPSite site = new SPSite(GetListSiteUrl()))
{
using (SPWeb web = site.OpenWeb())
{
// retrieve the list
SPList list = web.Lists[_ListName];
// more code to create the query...
items = list.GetItems(query);
}
}
}
catch (Exception e)
{
// log error
}
return items;
}
Run Code Online (Sandbox Code Playgroud)
编辑09/09/09
我主要指的是Stefan Grossner的这篇文章:
在最后一次访问此对象的子对象后,应该释放SPWeb或SPSite对象.
我相信他所说的是,如果我在处理过去的SPWeb之后使用SPListItemCollection ...... SPWeb将自动重新打开.
我直接询问Stefan后发现SPListItemCollection确实可以在你处理掉它后重新打开SPWeb。这意味着我上面发布的代码不正确,我只能在使用 SPListItemCollection 后才能处理 SPWeb。
更新:最好将 SPListItemCollection 转换为其他内容并返回它。
private DataTable GetListItems()
{
DataTable table = null;
try
{
SPListItemCollection items = null;
using (SPSite site = new SPSite(GetListSiteUrl()))
{
using (SPWeb web = site.OpenWeb())
{
// retrieve the list
SPList list = web.Lists[_ListName];
// more code to create the query...
items = list.GetItems(query);
// convert to a regular DataTable
table = items.GetDataTable();
}
}
}
catch (Exception e)
{
// log error
}
return table;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5931 次 |
| 最近记录: |