To establish a connection to a JSON data source the ADO.NET provider can be used through the C1JsonConnection class. This class requires a connection string to be provided as an argument, which can be created in either of the following ways:
The following code shows how a connection string can be configured by the C1JsonConnectionStringBuilder class and consumed by the C1JSsonConnection class to create a connection to JSON. Through that connection, the data source can be queried, updated etc.
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); |
Alternatively, the connection string can be written directly as a string literal, like in the following code. (a 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); |
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 the DataModel and JsonPath properties which are necessary to connect to your data source.
To connect to a 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'"; |
To connect to HTTP JSON Streams, 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'"; |