SQL IN Operator Syntax and Usage
Note
This feature is only available in NCache Enterprise Edition.
In order to search the cache using SQL like query format using IN operator, you can specify multiple attribute values after the IN keyword which means that ExecuteReader
will search the cache for all the attributes provided after the IN keyword.
Pre-Requisites
- Install the following NuGet packages:
- 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
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: ICache, ICacheReader, ExecuteReader, SearchService, QueryCommand.
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Syntax
Here's an example which retrieves Product_Name and UnitsAvailable from the cache having either 10,15,20,25 UnitsAvailable 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 ProductName, UnitsAvailable From FQN.Product WHERE UnitsAvailable IN (10,15,20,25)";
// 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
string ProductName = reader.GetValue<string>(1);
int UnitsAvailable = reader.GetValue<int>(2);
}
}
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
}
Additional Resources
NCache provides sample application for SQL Searching on GitHub.
See Also
Locking Data For Concurrency Control
SQL Search for Keys Syntax and Usage
SQL Search for Keys Syntax and Usage
SQL Like Operator Syntax and Usage
SQL GROUP BY Syntax and Usage
Query Operators
Search Cache with LINQ