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

    To create a connection in ADO.NET Provider, you can use C1JsonConnection class which implements a connection string passed as an argument. Alternatively, you can connect with the data source using the C1JsonConnectionStringBuilder class which builds and parses connection strings through code. The C1JsonConnectionStringBuilder class can also be used to setup the connection strings to be implemented by the provider for connecting to the data source.

    The following code shows how C1JsonConnectionStringBuilder class can be used to configure the connection string for Json and consumed by C1JsonConnection class to create a connection to Json server. Here, you can query the data.

    C#
    Copy Code
    //JSON service uri
    const string docUri = @"json_bookstore.json";
    
    //configure connection string
    C1JsonConnectionStringBuilder connectionStringBuilder = new C1JsonConnectionStringBuilder();
    connectionStringBuilder.DataModel = "Document";
    connectionStringBuilder.Uri = docUri;
    connectionStringBuilder.JsonPath = "$.bookstore.books";
               
    //Setup Connection
    C1JsonConnection con = new C1JsonConnection(connectionStringBuilder.ConnectionString);
    

    You can also create the connection string directly as shown in the code example below. The similar approach can be implemented in other DataConnectors for ADO.NET provider.

    C#
    Copy Code
    //Create connection string
    static string documentConnectionString = $"Data Model=Document;Uri='json_bookstore.json';Json Path='$.bookstore.books'";
    
    //Setup Connection
    C1JsonConnection con = new C1JsonConnection(documentConnectionString);
    

    JSON Data Sources

    The ADO.NET provider for JSON allows you to connect to local and https JSON resources. For connecting to these resources, you need to set the Uri property to the JSON resource location in addition to DataModel and JsonPath properties which are necessary to connect to your data source.

    Connect to Local Files

    To connect to local file, you can create a connection string by setting the Uri property to JSON file in addition to the DataModel and JsonPath properties as shown in the following code:

    C#
    Copy Code
    static string documentConnectionString = $"Data Model=Document;Uri='json_bookstore.json';Json Path='$.bookstore.books'";
    

    Connect to HTTP JSON Streams

    To connect to local file, you can create a connection string by setting the Uri property to the HTTP or HTTPS URL of the JSON resource you want to access as a table in addition to the DataModel and JsonPath properties, as shown in the following code:

    C#
    Copy Code
    static string documentConnectionString = $"Data Model=Document;Uri='https://raw.githubusercontent.com/sassy224/ZipFiles/master/json_bookstore.json';Json Path='$.bookstore.books'";