我有一个包含一些行的DataTable。有没有办法对其执行CAML查询?
或者,如何将此DataTable转换为SPList,以便CAML可以在SPList上执行。
private DataTable Process(DataTable d, string s)
{
DataTable dd = d;
SPQuery query = new SPQuery();
query.Query = s;
query.ViewAttributes = "Scope = 'RecursiveAll'";
DataTable dtSearch = new DataTable();
SPList Dest = new SPList();
dtSearch = d.GetItems(query).GetDataTable();
return dtSearch;
} 在上面的代码中,d.GetItems是做不到的。???
发布于 2015-09-11 08:59:48
我会尝试使用datatable支持的api来搜索数据表。例如,下面的msdn文章介绍了按主键搜索行和按列值搜索行。
https://msdn.microsoft.com/en-us/library/y06xa2h1.aspx
我希望这能帮到你。
发布于 2015-09-11 18:11:13
我在technet上试过了。
private SPList dtConvertSPList(DataTable dt)
{
SPList newList = null;
using (SPSite site = new SPSite("http://sharepointvm"))
{
using (SPWeb web = site.OpenWeb())
{
Guid newListGuid = web.Lists.Add("TempList", "This is my temp list, it will be removed when used", SPListTemplateType.GenericList);
newList = web.Lists[newListGuid];
if (newList != null)
{
/* Loop through the datatable and add columns to the list */
foreach (DataColumn dc in dt.Columns)
{
newList.Fields.Add(dc.ColumnName, SPFieldType.Text, false);
}
/* Update the list to get the new fields */
newList.Update();
/* Populate the list from the datatable */
SPListItem newItem = null;
foreach (DataRow row in dt.Rows)
{
/* Create a new item in the list */
newItem = newList.Items.Add();
foreach (DataColumn dc in dt.Columns)
{
newItem[dc.ColumnName] = row[dc].ToString();
newItem.Update();
}
}
}
}
}
return newList;
}但我面临的问题是:外部for循环,即
foreach (DataRow row in dt.Rows)
{
/* Create a new item in the list */
newItem = newList.Items.Add();
foreach (DataColumn dc in dt.Columns)
{
newItem[dc.ColumnName] = row[dc].ToString();
newItem.Update();
}
}这个外部循环只执行一次。
*调试结果:传递给此函数的DataTable有3358条记录。newList创建得很好,包含所有列,但只有一行,它的列中也有不完整的信息。
需要改进的地方:
循环应该工作得很好,我应该把代码放在哪里,以便在返回后删除这个newList?
https://stackoverflow.com/questions/32497152
复制相似问题