In Microsoft Sharepoint, the SPQuery class is used to build query strings to filter / retrieve specific set of data programmatically from a SP List. The query strings created this way does not resemble a SQL syntax as much as it resembles a XSLT query object, where everything gets formatted as XML strings.
For instance, let us take a State List which holds all the states of India. States are grouped under different zones i.e. North, South, East & West. If I need to retrieve all the States for North zone then my SP Query would look somewhat like,
SPQuery stateQuery = new SPQuery();
"<Where><Eq><FieldRef Name=\"Zone\"/><Value Type=\"Text\">North</Value></Eq></Where>";
SPListItemCollection stateCol = StateList.GetItems(stateQuery);
In the code above the StateCollection object gets populated with the States only related to North zone. You can then bind this collection to a GridView or DataList or any other similar controls to view the actual values.
– Each tag must be properly ended / closed.
– Eq stands for Equal. One cab also use NEq for not equal conditions.
– FieldRef holds the column name on which we want to run the query. In our case it is Zone.
– Value holds the parameter for the query column. In our case it is North.
– Can use / surround column name with / tags to have the corresponding NULL condition checks.