Show / Hide Table of Contents

    IDataStore.ImportJsonByKey(string json, DwBuffer dwbuffer, int startRow, int endRow) Method

    .NET Standard 2.x | Current Version (1.0.1)

    0.5.0-alpha

    1.0.1 (current)

    Inserts data from a JSON string into the specified buffer of DataStore according to the key name of the JSON item. You can specify the starting and ending positions in the JSON array.

    Namespace: PowerBuilder.Data

    Assembly: PowerBuilder.Data.dll

    Syntax

      public int ImportJsonByKey(string json, DwBuffer dwbuffer, int startRow, int endRow);
    

    Parameters

    json System.String

    A string specifying the JSON data.

    The JSON string must comply with the plain-format JSON or DataWindow JSON.

    dwbuffer PowerBuilder.Data.DwBuffer

    The specified buffer of the DataStore.

    A value of the DwBuffer enumerated datatype identifying the DataWindow buffer into which you want to import the data.

    For plain-format JSON: Imports the JSON data to the specified buffer.

    For DataWindow JSON: Imports data from the specified buffer from the JSON string to the corresponding buffer.

    startRow System.Int32

    The zero-based number of the first row in the JSON array that you want to import. If it is negative, 0 is used.

    endRow System.Int32

    The zero-based number of the last row in the JSON array that you want to import. If it is negative, it indicates the rest of rows.

    Returns

    System.Int32

    Returns the number of rows that were imported if it succeeds.

    Examples

    The following code example demonstrates how to import the department data from between the starting row and the ending row of a JSON string to the specified DataStore buffer.

    using PowerBuilder.Data;
    using System;
    using System.IO;
    
    namespace Appeon.ApiDoc.IDataStoreExamples
    {
        public class ImportJsonByKeyExample
        {
            private SchoolContext _context;
    
            public ImportJsonByKeyExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example4()
            {
                // Instantiates the datastore with datawindow: d_department2
                var datastore = new DataStore("d_department2", _context);
    
                // Gets the JSON file. ImportJson8.json contains 4 records.
                string path = Path.Combine(AppContext.BaseDirectory,
                                    @"Jsons\IDatastore\ImporttJson8.json");
                string json = File.ReadAllText(path);
    
                // Imports data from JSON to the DataStore primary buffer, from row 1 
                // through the end.
                // The departmentid column exists in ImportJson8.json, but not in 
                // DataStore d_department2.
                // But ImportJsonByKey imports data by the column name, so departmentid 
                // will be ignored.
                datastore.ImportJsonByKey(json, DwBuffer.Primary, 0, 3);
    
                Console.WriteLine("Rowcount: {0}", datastore.RowCount);
    
                for (int row = 0, rowcount = datastore.RowCount; row < rowcount; row++)
                {
                    Console.WriteLine("Name: {0}; Budget: {1}",
                                      datastore.GetItem<string>(row, "name"),
                                      datastore.GetItem<decimal>(row, "budget"));
                }
                datastore.Reset();
    
                // Imports data from row 2 and 3.
                // The departmentid column exists in ImportJson8.json, but not in DataStore 
                // d_department2.
                // But ImportJsonByKey imports data by the column name, so departmentid 
                // will be ignored.
                datastore.ImportJsonByKey(json, DwBuffer.Primary, 1, 2);
    
                Console.WriteLine();
                Console.WriteLine("Rowcount: {0}", datastore.RowCount);
    
                for (int row = 0, rowcount = datastore.RowCount; row < rowcount; row++)
                {
                    Console.WriteLine("Name: {0}; Budget: {1}",
                                      datastore.GetItem<string>(row, "name"),
                                      datastore.GetItem<decimal>(row, "budget"));
                }
    
                /*This code produces the following output:
    
                Rowcount: 4
                Name: Engineering; Budget: 350000
                Name: English; Budget: 120000
                Name: Economics; Budget: 200000
                Name: Mathematics; Budget: 250000
    
                Rowcount: 2
                Name: English; Budget: 120000
                Name: Economics; Budget: 200000
               */
            }
        }
    }
    

    Example Refer To

    JSON File: ImportJson8

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon