Delete Data with ExecuteNonQuery
NCache lets you delete the data from the cache based on the given criteria. The Delete
statement returns the number of the deleted rows through the query executed using ExecuteNonQuery
.
Important
The Delete
statement can only be executed through ExecuteNonQuery
.
Prerequisites to Delete Data with ExecuteNonQuery
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
- For API details, refer to: ICache, ICacheReader, ExecuteReader, SearchService, QueryCommand, ExecuteNonQuery.
Here's an example that deletes all the products from the cache with the UnitsInStock equals to 0 using ExecuteNonQuery
.
// Precondition: Cache is already connected
// Items are already present in the cache
// Provide Fully Qualified Name (FQN) of your custom class
string query = "DELETE FROM FQN.Product WHERE UnitsInStock = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("UnitsInStock", 0);
// Execute query
_cache.SearchService.ExecuteNonQuery(queryCommand);
Note
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 a sample application for SQL Queries on GitHub.
See Also
.NET: Alachisoft.NCache.Client.Services namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.client.services class.
Node.js: Cache class.