SQL ORDER BY Syntax and Usage
Note
This feature is only available in NCache Enterprise Edition.
NCache provides you with the ability to sort the data according to the given criteria through SQL like query format using ORDER BY clause. The ORDER BY
keyword lets you sort the result set in ascending or descending order as specified.
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
The following example sorts the products from the cache with UnitPrice greater than 100 based on their UnitsInStock sorted in descending order using ExecuteReader.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Provide Fully Qualified Name (FQN) of your custom class
string query = "SELECT * FROM FQN.Product WHERE UnitPrice > 100 ORDER BY UnitsInStock DESC";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
int result = reader.GetValue<int>("UnitsInStock");
}
}
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:
// Duplicate values in the ORDERBY clause
// Same column to be selected twice
// 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 sorts the products from the cache with UnitPrice greater than 100 based on their UnitsInStock sorted in descending order using ExecuteReader.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Specify the query with the specified criteria
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "SELECT FQN.Product WHERE this.UnitPrice > ? ORDER BY this.UnitsInStock DESC";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.ExecuteReader(queryCommand);
// Providing parameters for query
queryCommand.Parameters.Add("UnitPrice",100);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
// UnitsInStock lies on the 4th index
Product result = reader.GetValue<Product>(4);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else if (ex.ErrorCode == 25011)
{
// Same column cannot be selected twice
}
else if (ex.ErrorCode == 25003)
{
// Duplicate values in ORDERBY clause
}
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
Locking Data For Concurrency Control
SQL Search for Objects
SQL Search for Keys Syntax and Usage
SQL IN Operator Syntax and Usage
SQL Like Operator Syntax and Usage
Query Operators
Search Cache with LINQ