DataConnector | ComponentOne
ADO.NET provider for Dynamics 365 Sales / Caching
In This Topic
    Caching
    In This Topic

    Caching data offers several advantages that can improve overall process performance, including reduced API requests and faster data access.

    The ADO.NET provider features an easy-to-use caching procedure that can also be shared by multiple connections. This article demonstrates the two types of caches that are currently supported:

    To enable the caching feature, necessary connection properties are available, such as:

    Internal caching

    The following code demonstrates how to enable caching for a table by setting the UseCache property to True (by default, it is set to False). The cached data is stored in the file specified by the CacheLocation property in the connection string. For more information on Incremental Caching, refer to this topic.

    C#
    Copy Code
    static void LocalCache()
    {
        //Define connection string
        string connectionString = $@"Url = {urlDynamics}; Use Pool = true; Use Cache = true; Cache Tolerance = 6; 
               Cache Location='C:\temp\c1cache.db'; OAuth Client Id = {clientID}; Username = {username}; Password = {password}; 
               OAuth Token Endpoint = {tokenEnpoint}; OAuth Extend Properties = {extendProperties}; Max Page Size = 100;";
               
        Console.WriteLine("Start Time: " + DateTime.Now);
    
        using (var conn = new C1D365SConnection(connectionString))
        {
            conn.Open();
            var cmd = new C1D365SCommand(conn, "SELECT accountid, Name FROM Accounts LIMIT 12'");
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine("Read and cached the row with AccountId " + reader["AccountId"]);
            }
        }
    
        Console.WriteLine("End Time: " + DateTime.Now);
    }

    External caching

    The ADO.NET provider for Dynamics 365 Sales supports external caching, allowing users to store cached data in a separate database. This external database serves as a constant repository and can be accessed by multiple connection objects simultaneously. It includes all tables exposed by the Dynamics 365 Sales provider and offers configuration options specific to the cache provider.

    To utilize external caching, the provider supports a specified cache, such as SQL Server. You can specify the connection string using the UseCache, CacheProvider, and CacheConnection keys to enable external caching.

    The following code example implements external caching, by setting Use Cache to True and using 'Microsoft.Data.SqlClient' as the cache provider.

    C#
    Copy Code
    static void ExternalCache()
    {
        // Explicit MS SQL Cache Config
        string connectionString = $@"Url = {urlDynamics}; Use Cache = true; Cache provider = 'Microsoft.Data.SqlClient'; 
                            Cache connection = 'Server=yourserverid;Database=databasename;User Id=yourId;Password=yourpassword;' 
                            OAuth Client Id = {clientID}; Username = {username}; Password = {password}; OAuth Token Endpoint = {tokenEnpoint}; OAuth Extend Properties = {extendProperties}; Max Page Size = 100;";
    
        Console.WriteLine("Start Time: " + DateTime.Now);
    
        using (var con = new C1D365SConnection(connectionString))
        {
            con.Open();
    
            var cmd = new C1D365SCommand(connection,"SELECT accountid, Name FROM Accounts LIMIT 12");
    
            var rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                Console.WriteLine("Read and cached the row with AccountId " + rdr["AccountId"]);
            }
        }
    
        Console.WriteLine("End Time: " + DateTime.Now);
    }
    Note: With SQL Server, "Cache Provider = 'Microsoft.Data.SqlClient'" is mandatory and the user should have created their own database because the provider doesn’t create one.