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

    To establish a connection to Dynamics 365 sales data source, the ADO.NET Provider can be used through the C1D365SConnection class. This class requires a connection string to be provided as an argument, which can be created in either of the following ways:

    Connection string generation.

    The code example below shows how a connection string can be configured by the C1D365SConnectionStringBuilder class and consumed by the C1D365SConnection class to create a connection to the Dynamics 365 Sales server. Through that connection, the data source can be queried, updated, etc.

    C#
    Copy Code
    var builder = new C1D365SConnectionStringBuilder();
    builder.Url = urlDynamics;
    builder.Username = username;
    builder.Password = password;
    builder.OAuthTokenEndpoint = tokenEnpoint;
    builder.OAuthExtendProperties = extendProperties;
    builder.OAuthClientId = clientID;
    
    // Connection attributes can be referred to by string
    builder["Max Page Size"] = 100;
    builder["Use Cache"] = true;
    
    // Create Connection passing connection string
    var con = new C1D365SConnection(builder.ConnectionString); 
    Console.WriteLine("Connection Created using C1D365SConnectionStringBuilder Class:");
    Console.WriteLine("Created Connection String is:\n" + builder.ConnectionString);
    Console.ReadKey();

    Connection string literal

    Alternatively, the connection string can be written directly as a string literal, like in the code example below (a similar approach can be implemented in other DataConnectors for ADO.NET provider).

    C#
    Copy Code
    // Whole connection string
    string connstr = $@"Url = {urlDynamics}; Use Cache = true; Use Etag = true; OAuth Client Id = {clientID}; Username = {username}; Password = {password}; 
               OAuth Token Endpoint = {tokenEnpoint}; OAuth Extend Properties = {extendProperties}; Max Page Size = 100";
    
    // Setup the connection
    var con = new C1D365SConnection(connstr);

    Note: Connection strings can be formed by using as connection keys the names of the corresponding properties of C1D365SConnectionStringBuilder but with an added space character. For example, to set the property UseCache, the connection key should be written as UseCache.