DataConnector | ComponentOne
ADO.NET provider for JSON / Querying Data
In This Topic
    Querying Data
    In This Topic

    The ADO.NET Provider for JSON implements two classes you can use to perform CRUD (Create, Read, Update, and Delete)operations: DbDataReader and C1JsonDataAdapter classes. The tabs below describe the interfaces and code implementations.

    The provider supports CRUD (Create, Read, Update, Delete) actions for both local JSON files and Web APIs. This functionality is explained in the following sections. It is important to note that when working with Web API JSON streams, configuration files are required for performing CRUD operations. You can either use an existing configuration file or create a new one. For detailed instructions on creating a configuration file, please refer to the Configuration section.

    Querying With DbDataReader

    The DbDataReader class can be used to fetch data in subset increments as required. It can retrieve data quicker than the C1JsonDataAdapter as it retrieves data in pages. When you read data from the DbDataReader, it requests the succeeding page from the data source to load the result, which makes the data retrieval faster.

    The following code examples demonstrate read, create, update, and delete operations from the data source using the DbDataReader.

    Create Data

    The example adds new records by executing the Insert command.

    C#
    Copy Code
    static void InsertDataReader()
    {
        string connectionString = string.Format(@"Data Model={0};Uri='{1}';Json Path='{2}'; api config file = 'api_config.xml'; 
                       Use Pool=true; username='****'; password='*****'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Album");
                
        using (var con = new C1JsonConnection(connectionString))
        {
            con.Open();
            var sqlInsert = "Insert Into Album([AlbumId], [Title], [ArtistId]) values (9999667,'test', 1)";
            var cmdInsert = con.CreateCommand();
            cmdInsert.CommandText = sqlInsert;
            var result1 = cmdInsert.ExecuteNonQuery();
        }
    }

    Read Data

    The example retrieves data from a JSON file named json_bookstore.json by executing the Select command.

    C#
    Copy Code
    static void ReadDataReader()
    {
        static string documentConnectionString = $"Data Model=Document;Uri='json_bookstore.json';Json Path='$.bookstore.books'";
        
        using (var con = new C1JsonConnection(documentConnectionString))
        {
            con.Open();
            // Create Read command
            var cmd = con.CreateCommand();
            cmd.CommandText = "Select * From books";
            
            // Execute Read command and display fetched data
            DbDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                Console.WriteLine(String.Format("\t{0} --> \t\t{1}", rdr["title"], rdr["ISBN"]));
            }
            Console.WriteLine("Read operation successful! \n\n");
        } 
    }      

    Update Data

    This example modifies data by executing the Update command.

    C#
    Copy Code
    static void UpdateDataReader()
    {
        string connectionString = string.Format(@"Data Model={0};Uri='{1}';Json Path='{2}';api config file = 'api_config.xml';
                       Use Pool=true; username='****'; password='*****'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Album");
                
        using (var con = new C1JsonConnection(connectionString))
        {
            con.Open();
            var sqlUpdate = "Update Album set [Title] = 'abcde' where [AlbumId] = 9999667";
            var cmdUpdate = con.CreateCommand();
            cmdUpdate.CommandText = sqlUpdate;
            var result2 = cmdUpdate.ExecuteNonQuery();
        }
    }      

    Delete Data

    This example removes data by executing the Delete command.

    C#
    Copy Code
    static void DeleteDataReader()
    {
        string connectionString = string.Format(@"Data Model={0};Uri='{1}';Json Path='{2}';api config file = 'api_config.xml';
                       Use Pool=true; username='****'; password='*****'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Invoice");
                
        using (var con = new C1JsonConnection(connectionString))
        {
            con.Open();
            var sqlDelete = "Delete from Album where [AlbumId] = 9999667";
            var cmdDelete = con.CreateCommand();
            cmdDelete.CommandText = sqlDelete;
            var result3 = cmdDelete.ExecuteNonQuery();
        }
    }

    Querying With C1JsonDataAdapter

    The C1JsonDataAdapter class can be used to retrieve a single result set containing all the data that matches a given query. The C1JsonDataAdapter uses its Fill method to fetch data from the data source. An empty DataTable instance is passed as an argument to the Fill method. Once the method returns, the DataTable instance is populated with the queried data. Since the Fill method must retrieve all the data from the data source before returning, the C1JsonDataAdapter is slower compared to the DbDataReader.

    The following code examples demonstrate create, read, update and delete operations from the data source using C1JsonDataAdapter.

    Create Data

    The example adds new records by executing the Insert command.

    C#
    Copy Code
    static void InsertDataAdapter() 
    {
        string connectionString = string.Format(@ "Data Model={0};Uri='{1}';Json Path='{2}'; api config file = 'api_config.xml'; 
                       Use Pool=true; username='******'; password='*******'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Album");
        
            using(C1JsonConnection conn = new C1JsonConnection(connectionString)) 
            {
            //Populate DataTable
            C1JsonDataAdapter adapter = new C1JsonDataAdapter(conn, "Select * from Album");
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
    
            //Create Insert command
            adapter.InsertCommand = new C1JsonCommand(conn);
            adapter.InsertCommand.CommandText = "Insert into Album (AlbumId, Title, ArtistId) values (@AlbumId, @Title, @ArtistId)";
            adapter.InsertCommand.Parameters.Add("@AlbumId", "AlbumId");
            adapter.InsertCommand.Parameters.Add("@Title", "Title");
            adapter.InsertCommand.Parameters.Add("@ArtistId", "ArtistId");
            adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
            
            //Insert new row
            DataRow albumRow = dataTable.NewRow();
            albumRow["AlbumId"] = 9999668;
            albumRow["Title"] = "test";
            albumRow["ArtistId"] = 1;
            dataTable.Rows.Add(albumRow);
    
            //Update database
            var i = adapter.Update(dataTable);
            if (i != -1) 
            {
                Console.WriteLine("Insert operation successful !!! \n \n");
            }
    
            //Uncomment to display and verify results
            ShowDataTable(dataTable);
        }
    }

    Read Data

    This example retrieves data by executing the Select command.

    C#
    Copy Code
    public void ReadDataAdapter()
    {
        string connectionString = string.Format(@"Data Model={0};Uri='{1}';Json Path='{2}'; api config file = 'api_config.xml'; 
                       Use Pool=true; username='*****'; password='******'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Album");
    
        using (C1JsonConnection conn = new C1JsonConnection(connectionString))
        {
            //Populate DataTable
            C1JsonDataAdapter adapter = new C1JsonDataAdapter();
    
            //Create Select command
            adapter.SelectCommand = new C1JsonCommand(conn);
            adapter.SelectCommand.CommandText = "Select * from Album";
            adapter.SelectCommand.UpdatedRowSource = UpdateRowSource.None;
    
            //Populate Datatable with results using DataAdapter
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
            Console.WriteLine("Select operation successful !!! \n \n");
    
            //Display results
            ShowDataTable(dataTable);
        }
    }

    Update Data

    This example modifies data by executing the Update command.

    C#
    Copy Code
    static void UpdateDataAdapter() 
    {
        string connectionString = string.Format(@ "Data Model={0};Uri='{1}';Json Path='{2}'; api config file = 'api_config.xml'; 
                       Use Pool=true; username='*******'; password='********'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Album");
        
            using(C1JsonConnection conn = new C1JsonConnection(connectionString)) 
            {
            //Populate DataTable
            C1JsonDataAdapter adapter = new C1JsonDataAdapter(conn, "Select * from Album");
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
    
            //Create Update command
            adapter.UpdateCommand = new C1JsonCommand(conn);
            adapter.UpdateCommand.CommandText = "UPDATE Album SET ArtistId=@ArtistId where AlbumId=@AlbumId";
            adapter.UpdateCommand.Parameters.Add("@ArtistId", "ArtistId");
            adapter.UpdateCommand.Parameters.Add("@AlbumId", "AlbumId");
            adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
    
            //Update existing row
            DataRow albumRow = dataTable.Rows[0];
            albumRow["ArtistId"] = 63;
    
            //Update database
            var i = adapter.Update(dataTable);
    
            if (i != -1) 
            {
                Console.WriteLine("Update operation successful !!! \n \n");
            }
    
            //Uncomment to display and verify results
            ShowDataTable(dataTable);
        }
    }

    Delete Data

    This example removes data by executing the Delete command.

    C#
    Copy Code
    static void DeleteDataAdapter() 
    {
        string connectionString = string.Format(@ "Data Model={0};Uri='{1}';Json Path='{2}'; api config file = 'api_config.xml'; 
                       Use Pool=true; username='*********'; password='*******'", "Relational", "http://45.125.239.138:8088/api/Album", "$.Album");
        
            using(C1JsonConnection conn = new C1JsonConnection(connectionString)) 
            {
            //Populate DataTable
            C1JsonDataAdapter adapter = new C1JsonDataAdapter(conn, "Select * from Album");
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
    
            //Create Delete command
            adapter.DeleteCommand = new C1JsonCommand(conn);
            adapter.DeleteCommand.CommandText = "Delete from Album where AlbumId = @AlbumId";
            adapter.DeleteCommand.Parameters.Add("@AlbumId", "AlbumId");
            adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
    
            //Delete a row
            DataRow albumRow = dataTable.Rows[dataTable.Rows.Count - 1];
            albumRow.Delete();
    
            //Update database
            var i = adapter.Update(dataTable);
    
            if (i != -1) 
            {
                Console.WriteLine("Delete operation successful !!! \n \n");
            }
    
            //Uncomment to display and verify results
            ShowDataTable(dataTable);
        }
    }