DataConnector | ComponentOne
ADO.NET provider for OData / Creating Connection
In This Topic
    Creating Connection
    In This Topic

    To create a connection to an OData data source, the ADO.NET Provider can be used through the C1ODataConnection 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 C1OdataConnectionStringBuilder class and consumed by the C1ODataConnection class to create a connection to the OData server. Through that connection, the data source can be queried, updated, etc.

    C#
    Copy Code
    // URL of the service at odata.org
    const string NorthwindSampleUrl = "https://services.odata.org/V4/Northwind/Northwind.svc";
    
    // Configure the connection string
    var builder = new C1ODataConnectionStringBuilder();
    builder.Url = NorthwindSampleUrl;
    builder.UsePool = true;
    
    // Setup the connection
    var con = new C1ODataConnection(builder.ConnectionString);

    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
    var connectionString = "Url=https://services.odata.org/V4/Northwind/Northwind.svc; Use Pool=true";
    
    // Setup the connection
    var con = new C1ODataConnection(connectionString);

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

    OData versions

    By default, the provider assumes that the latest version of OData (version 4) is being used. If a different version of OData is used, the "odata version" property needs to be set to the specific OData version in the connection string. For example, if OData version 2 is being used, "odata version=2" needs to be specified in the connection string.

    Example Title
    Copy Code
    var connectionString = "https://services.odata.org/V2/(S(f5su0fte55y534onqz3zvfgf))/OData/OData.svc/; odata version = 2";