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
- Create a new Console Application.
- Install either of the following NuGet packages in your .NET client application:
- Enterprise:
Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
- Professional:
Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
- Make sure that the data being added is serializable.
- Add NCache References by locating
%NCHOME%\NCache\bin\assembly\4.0
and adding Alachisoft.NCache.Web
and Alachisoft.NCache.Runtime
as appropriate.
- Include the
Alachisoft.NCache.Web.Caching
namespace in your application.
- To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.
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);
// 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
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("UnitsInStock", 0);
// Execute query
int affectedRows = cache.getSearchService().executeNonQuery(queryCommand);
System.out.println(affectedRows + " rows were affected by the delete operation.");
# Precondition: Cache is already connected
# Items are already present in the cache
# Provide Fully Qualified Name(FQN) of your custom class
query = "DELETE FROM FQN.Product WHERE units_in_stock = 0"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Execute query
cache.get_search_service().execute_non_query(query_command)
// This is an async method
// Precondition: Cache is already connected
// Items are already present in the cache
// Provide Fully Qualified Name (FQN) of your custom class
var query = "DELETE FROM FQN.Product WHERE UnitsInStock = ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
//Providing parameters for query
let parameter = new Map();
parameter.set("UnitsInStock", 500);
queryCommand.setParameters(parameter);
// Execute query
await ( await this.cache.getSearchService()).executeNonQuery(queryCommand);
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected
// Items are already present in the cache
// Provide Fully Qualified Name (FQN) of your custom class
string query = "DELETE Alachisoft.NCache.Sample.Data.Product WHERE this.UnitsInStock = ?";
// Set parameters
Hashtable parameters = new Hashtable();
parameters.Add("UnitsInStock", 0);
// Execute query
int affectedRows = cache.ExecuteNonQuery(query, parameters);
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.