DataEngine for .NET Standard | ComponentOne
Work with Data Engine / Connect Data Engine / Import Data from CSV
In This Topic
    Import Data from CSV
    In This Topic

    With the Data Engine library, you can import data from CSV files to the DataEngine table into a queryable collection of objects in your code.

     Note:  For importing data from a CSV file to DataEngine table, you need to add the CsvHelper NuGet package in your application, apart from the C1.DataEngine.Core and C1.DataEngine.Core.Api NuGet packages. 

    To connect Data Engine to CSV files, you need to follow the steps given below:

    1. Initialize a new workspace folder relative to the project root directory using the Init method of the Workspace class.

      //Initialize a new workspace folder relative to the project root directory
      Workspace workspace = new Workspace();
      workspace.Init("workspace");
    2. Create a class containing properties that can hold the data corresponding to the fields in the CSV file.

      class VideoGameSales
      {
          public int Rank { get; set; }
          public string Name { get; set; }
          public string Platform { get; set; }
          public string ReleaseYear { get; set; }
          public string Genre { get; set; }
          public string PublisherName { get; set; }
          public double GlobalSales { get; set; }
      }
    3. In order to map the property names of a class to the header names of the CSV data, you can create a mapping class as shown.

      Note: You need to create a mapping class only if you are using a class containing property names different from the header names in the CSV file.
      sealed class VGSalesMap : ClassMap<VideoGameSales>
      {
          public VGSalesMap()
          {
              Map(m => m.Rank).Name("rank");
              Map(m => m.Name).Name("name");
              Map(m => m.Platform).Name("platform");
              Map(m => m.ReleaseYear).Name("year");
              Map(m => m.Genre).Name("genre");
              Map(m => m.PublisherName).Name("publisher");
              Map(m => m.GlobalSales).Name("global_sales");
          }
      }
    4. Read CSV data and transform it into an IEnumerable collection of custom .NET objects using the classes and methods available in the CsvHelper Nuget package as shown:

      //Read CSV data and convert it into a collection of custom .NET objects
      var reader = new StreamReader("VideoGameSales.csv");
      var csv = new CsvReader(reader);
      csv.Configuration.RegisterClassMap<VGSalesMap>(); //Simplifies the mapping of CSV field headers to .NET-compatible names. 
      var videoGamesList = csv.GetRecords<VideoGameSales>().AsEnumerable();
    5. To connect the DataEngine to the retrieved collection of custom .NET objects, create an instance of the ObjectConnector class and pass the Workspace object and IEnumerable collection initialized above, as parameters to its constructor. Use the ObjectConnector’s GetData method to create a DataEngine table containing the imported data.

      //Import CSV data from custom collection to a DataEngine table           
      ObjectConnector<VideoGameSales> connector = new ObjectConnector<VideoGameSales>(workspace, videoGamesList);
      connector.GetData("VideoGameSales");

      Once the connection is established, the queries can be defined and executed to fetch the desired data. Refer to Transform Data topic for more details.