SQL Logical Operators Usage
Note
This feature is only available in NCache Enterprise Edition.
You can also use logical operators AND, OR, NOT in your query through SQL like query format. These operators are used for specifying more than one condition after the operator. Whereas the NOT operator displays the result if the condition is not true.
Pre-Requisites
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Client.Services
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Modern Syntax
Here's an example showing how to use the combination of AND and OR operator in a single query within the WHERE clause using ExecuteReader. The following query retrieves all the products which belong to the category Beverages and have UnitsInStock greater than 0 or all the products which belong to the category Produce having UnitPrice greater than 100.
try
{
// Pre-condition: Cache is already connected
// Provide Fully Qualified Name (FQN) of your custom class
string query = "SELECT * FROM FQN.Product WHERE (Category = 'Beverages' AND UnitsInStock > 0) OR (Category = 'Produce' AND UnitPrice > 100) ";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string result = reader.GetValue<string>("ProductID");
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Legacy Syntax
Note
Legacy API is only available in NCache Enterprise Edition.
The following example shows how to use the combination of AND and OR operator in a single query within the WHERE clause using ExecuteReader. The following query retrieves all the products which belong to the category Beverages and have UnitsInStock greater than 0 or all the products which belong to the category Produce having UnitPrice greater than 100.
try
{
// Pre-condition: Cache is already connected
// Create a query which will be executed on the data set
// Instead of Product, specify Fully Qualified Name (FQN) of your custom class
string query = "SELECT FQN.Product WHERE (this.Category = ? AND this.UnitsInStock > ?) OR (this.Category = ? AND this.UnitPrice > ?) ";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("Category","Beverages");
queryCommand.Parameters.Add("UnitsInStock",0);
queryCommand.Parameters.Add("Category","Produce");
queryCommand.Parameters.Add("UnitPrice",100);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Product result = reader.GetValue<Product>(1);
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for SQL Searching at:
Shipped with NCache: %NCHOME%\samples\dotnet\SearchUsingSQL
See Also
SQL Search for Keys Syntax and Usage
SQL IN Operator Syntax and Usage
SQL Like Operator Syntax and Usage
SQL GROUP BY Syntax and Usage
Query Operators
Search Cache with LINQ