DataConnector | ComponentOne
ADO.NET provider for JSON / Getting Started
In This Topic
    Getting Started
    In This Topic

    ADO.NET Provider for JSON offers effortless way to connect to JSON data using ADO.NET or EntityFramework Core. It wraps the complexity of using JSON data and lets you query flat & relational JSON data. In addition, it supports querying JSON data using SQL or LINQ.

    DataConnector offers everything from wide range of data connection techniques using ADO.NET provider for JSON. This documentation helps you get started with C1.DataConnector control which gives an idea about different ways of data connection and operations with JSON.

    This quick start will guide you through the steps of using DataConnector in a console application. Complete the steps given below to see how DataConnector works.

    Step 1: Create a new Console App

    1. Open Visual Studio.
    2. Select Create a new project from the Get started pane.
    3. In the Create a new project window, select Console Application and click Next.
      Create new project window
    4. In the Configure your new project window, write your project name, choose location to save your project and click Create.

    Step 2: Add the NuGet packages

    1. From the Project menu, select Manage NuGet Packages. The NuGet Package Manager appears.
    2. Select nuget.org from the Package source drop down.
    3. Click Browse tab and select C1.AdoNet.Json from the left pane.
    4. In the right pane, click Install. This adds the references for the above packages.

    Step 3: Using ADO.Net provider for JSON

    Follow the steps provided below to learn and implement data retrieval using ADO.NET provider for JSON.

    1. Create a connection string to set up a connection to a local JSON resource by setting Uri, DataModel and JsonPath properties.
      C#
      Copy Code
      static string documentConnectionString = $"Data Model=Document;Uri='json_bookstore.json';Json Path='$.bookstore.books'";
      
    2. Fetch the data using C1JsonConnection class. The connection string using corresponding attributes is passed as an argument. For more information on creating connections, see Creating Connection.
      C1JsonConnection implements the ADO.NET DbConnection, similar to standard ADO.NET connection object that retrieves a single result set of all the data that matches a query. Once the connection is established, it retrieves the data from the source as shown in the below code example.
      C#
      Copy Code
      Console.WriteLine("Query all Accounts...");
      //Fetch data
      using(var con = new C1JsonConnection(documentConnectionString))
      {
          con.Open();
      
          var table = con.GetSchema("columns", new string[] { "books" });
          ShowDataTable(table);
      
          var cmd = con.CreateCommand();
          //Provide command
          cmd.CommandText = "Select * From books";
          var reader = cmd.ExecuteReader();
      }
       
      //Display Table
      static void ShowDataTable(DataTable table, int length = 25)
      {
          foreach (DataColumn col in table.Columns)
          {
              Console.Write("{0,-" + length + "}", col.ColumnName);
          }
          Console.WriteLine();
      
          foreach (DataRow row in table.Rows)
          {
              foreach (DataColumn col in table.Columns)
              {
                  if (col.DataType.Equals(typeof(DateTime)))
                      Console.Write("{0,-" + length + ":d}", row[col]);
                  else if (col.DataType.Equals(typeof(decimal)))
                      Console.Write("{0,-" + length + ":C}", row[col]);
                  else
                      Console.Write("{0,-" + length + "}", row[col]);
              }
             Console.WriteLine();
          }
      }
      

    The following output is generated on the console application on executing the above steps.