• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Delete Data with ExecuteNonQuery

ExecuteNonQuery is an API in NCache that uses a SQL-like DELETE statement to remove data on the basis of specified criteria (indexed attributes). ExecuteNonQuery efficiently deletes all items that meet a query clause (e.g., DELETE FROM Product WHERE UnitsInStock = 0) and returns the number of impacted rows, in contrast to the conventional Remove function, which requires specified keys.

Important

The Delete statement can only be executed through ExecuteNonQuery.

Prerequisites

Before using the NCache client-side APIs, ensure that the following prerequisites are fulfilled:

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
  • Install the following NuGet packages in your .NET client application:
    • Enterprise: Alachisoft.NCache.SDK
    • OpenSource: Alachisoft.NCache.Opensource.SDK
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Exceptions
    • Alachisoft.NCache.Client.Services
    • Alachisoft.NCache.Runtime.Caching
  • Make sure that the data being added is serializable.
  • 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.
  • Add the following Maven dependencies for your Java client application in pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <!--for NCache Enterprise-->
    <artifactId>ncache-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Import the following packages in your Java client application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
    • import com.alachisoft.ncache.runtime.caching.*;
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: Cache, CacheReader, executeReader, getSearchService, QueryCommand, executeNonQuery.
  • Install the following packages in your Python client application:
    • Enterprise: ncache-client
  • Import the following packages in your application:
    • from ncache.client import*
    • from ncache.client.services import *
    • from ncache.runtime.caching import *
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: Cache, CacheReader, execute_reader, get_search_service, QueryCommand, execute_non_query.
  • Install and include the following module in your Node.js client application:

    • Enterprise: ncache-client
  • Include the following class in your application:

    • Cache
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: Cache, CacheReader, executeReader, getSearchService, QueryCommand, executeNonQuery.
  • 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
  • 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.

The following example deletes all products from the cache where UnitsInStock is equal to 0, using ExecuteNonQuery:

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
try
{
  // 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);
}
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 performed during state transfer
        // Operation Timeout
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException
} 
try
{
  // 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);
}
catch (OperationFailedException ex)
{

    if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure the query format is correct
    } 
    else
    {
        // Exception can occur due to:
        // Connection Failures
        // Operation performed during state transfer
        // Operation Timeout
    }
} 
catch (Exception ex)
{
    // Any generic exception like IllegalArgumentException or NullPointerException
}
try:
  # 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 = ?"

  # Use QueryCommand for query execution
  query_command = QueryCommand(query)
  query_command.set_parameters({"units_in_stock": 0})

  # Execute query
  rows_affected = cache.get_search_service().execute_non_query(query_command)

except ncache.OperationFailedException as ex:
  # NCache specific exception can occur due to:
  # - Connection failures
  # - Operation performed during state transfer
  # - Operation timeout
  pass

except Exception as ex:
  # Any generic exception
  pass
try
{
  // 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 QueryCommand(query);

  // Providing parameters for query
  let parameter = new Map();
  parameters.set("UnitsInStock", 0);
  queryCommand.setParameters(parameters);

  // Execute query
  const searchService = await cache.getSearchService();
  let rowsAffected = await searchService.executeNonQuery(queryCommand);
}
catch (error)
{
    // Handle errors
}
try
{
  // 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);
}
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 performed during state transfer
        // Operation Timeout
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException
} 
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.

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top