string
beginTime
=
txtFormDate.Text.Trim();
//
开始时间(用户选择)--查询条件
string endTime = txtToDate.Text.Trim(); // 结束时间(用户选择)--查询条件
string prodNum = txtProdNum.Text.Trim(); // 商品编码(用户填写)--查询条件
string shopName = txtShopName.Text.Trim(); // 门店名称(用户填写)--查询条件
string queryStr = string .Empty;
query = new SPQuery();
if (beginTime.Length > 0 && endTime.Length > 0 )
{
queryStr += string .Format( @" <And><Geq><FieldRef Name='SubmitDate'/><Value Type='DateTime'>{0}</Value></Geq>
<Leq><FieldRef Name='SubmitDate'/><Value Type='DateTime' >{1}</Value></Leq></And> " , beginTime, endTime);
}
if (beginTime.Length > 0 && endTime.Length <= 0 )
{
queryStr += string .Format( " <Geq><FieldRef Name='SubmitDate'/><Value Type='DateTime' >{0}</Value></Geq> " , beginTime);
}
if (beginTime.Length <= 0 && endTime.Length > 0 )
{
queryStr += string .Format( " <Leq><FieldRef Name='SubmitDate'/><Value Type='DateTime' >{0}</Value></Leq> " ,endTime);
}
if (prodNum.Length > 0 )
{
queryStr += string .Format( " <Eq><FieldRef Name='ProdNum'/><Value Type='Text'>{0}</Value></Eq> " , prodNum);
}
if (shopName.Length > 0 )
{
queryStr += string .Format( " <Eq><FieldRef Name='ShopName'/><Value Type='Text'>{0}</Value></Eq> " , shopName);
}
query.Query = string .Format( " <Where>{0}</Where> " , queryStr);
string endTime = txtToDate.Text.Trim(); // 结束时间(用户选择)--查询条件
string prodNum = txtProdNum.Text.Trim(); // 商品编码(用户填写)--查询条件
string shopName = txtShopName.Text.Trim(); // 门店名称(用户填写)--查询条件
string queryStr = string .Empty;
query = new SPQuery();
if (beginTime.Length > 0 && endTime.Length > 0 )
{
queryStr += string .Format( @" <And><Geq><FieldRef Name='SubmitDate'/><Value Type='DateTime'>{0}</Value></Geq>
<Leq><FieldRef Name='SubmitDate'/><Value Type='DateTime' >{1}</Value></Leq></And> " , beginTime, endTime);
}
if (beginTime.Length > 0 && endTime.Length <= 0 )
{
queryStr += string .Format( " <Geq><FieldRef Name='SubmitDate'/><Value Type='DateTime' >{0}</Value></Geq> " , beginTime);
}
if (beginTime.Length <= 0 && endTime.Length > 0 )
{
queryStr += string .Format( " <Leq><FieldRef Name='SubmitDate'/><Value Type='DateTime' >{0}</Value></Leq> " ,endTime);
}
if (prodNum.Length > 0 )
{
queryStr += string .Format( " <Eq><FieldRef Name='ProdNum'/><Value Type='Text'>{0}</Value></Eq> " , prodNum);
}
if (shopName.Length > 0 )
{
queryStr += string .Format( " <Eq><FieldRef Name='ShopName'/><Value Type='Text'>{0}</Value></Eq> " , shopName);
}
query.Query = string .Format( " <Where>{0}</Where> " , queryStr);
SPQuery查询语句是以XML格式展现出来的,如下面的例子<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
<Where>
<Or>
<Neq>
<FieldRef Name="Status"></FieldRef> //字段名
<Value Type="Text">Completed</Value> //字段值
</Neq>
<IsNull>
<FieldRef Name="Status"></FieldRef>
</IsNull>
</Or>
</Where>
</Query>
现在我用大家熟悉的SQL语法来和它做个比较
SPQuery | SQL |
<Where></Where> | Where |
<OrderBy></OrderBy> | Order By |
<And></And> | And |
<Or></Or> | Or |
<Eq></Eq> | = |
<Geq></Geq> | >= |
<Gt></Gt> | > |
<Leq></Leq> | <= |
<Lt></Lt> | < |
<Neq></Neq> | != |