SPquery

Guid guid = new Guid("{8238B046-EC5F-46B6-8E18-028F77EC2620}");
            SPSite Asite = new SPSite("http://mosslt/");
            SPList list = Asite.RootWeb.Lists[guid];

            SPQuery query = new SPQuery();
           query.Query = @"
                               <OrderBy>
                                    <FieldRef Name=ID' />
                               </OrderBy>

                               <Where>
                                    <Gt>
                                         <FieldRef Name='ID' />
                                         <Value Type='Counter'>0</Value>
                                    </Gt>
                               </Where>"; //顺序按ID排,条件为ID大于0

            query.ViewFields = @"            
                                  <FieldRef Name='LinkTitle' />
                                  <FieldRef Name='htchange' />
                                  <FieldRef Name='htinfo' />";//要显示在前的三字段,后而会自动回上ID,Created

 


            this.GridView1.DataSource =list.GetItems(query).GetDataTable();
            this.GridView1.DataBind();

字段及其它信息可以用Stramit Caml Viewer 2007 查看

 

 

 

 

如:

 

 

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
    SPList oList = oWebsiteRoot.Lists["List_Name"];

    SPQuery oQuery = new SPQuery();
    oQuery.ViewFields = "<FieldRef Name='Field1'/>" +
        "<FieldRef Name='Field2'/>";
    oQuery.Query = "<Where><Geq><FieldRef Name='Field2'/>" +
        "<Value Type='Number'>1000</Value></Geq></Where>";
    SPListItemCollection collListItems = oList.GetItems(oQuery);

    foreach (SPListItem oListItem in collListItems)
    {
        Response.Write(SPEncode.HtmlEncode(oListItem.Xml) + "<BR>");
    }
}

 

 

 

用法

 

using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Site_Name"])
{
    SPList oList = oWebsite.Lists["List_Name"];

    SPQuery oQuery = new SPQuery();

    oQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='ID'/>" +
        "</OrderBy><Where><Or><Geq><FieldRef Name='Field1'/>" +
        "<Value Type='Number'>1500</Value></Geq><Leq>" +
        "<FieldRef Name='Field2'/><Value Type='Number'>500</Value>" +
        "</Leq></Or></Where></Query><ViewFields>" +
        "<FieldRef Name='Title'/>" +
        "<FieldRef Name='Field1'/><FieldRef Name='Field2'/>" +
        "<FieldRef Name='Field3'/><FieldRef Name='Field4'/>" +
        "</ViewFields><RowLimit>100</RowLimit></View>";
    SPListItemCollection collListItemsAvailable = oList.GetItems(oQuery);

    foreach (SPListItem oListItemAvailable in collListItemsAvailable)
    {
        Response.Write(SPEncode.HtmlEncode(oListItemAvailable.Xml) +
          "<BR>");
    }
}

 

posted @ 2010-04-30 11:09  China2008  阅读(322)  评论(0编辑  收藏  举报