DwDataTableExtensions.GetDataImporter(DataFormat dataFormat) Method
.NET Standard 2.x
Gets the DataTable importer. You need to specify the data import DataFormat
.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public static IDataImporter GetDataImporter(this DataTable dataTable, DataFormat dataFormat)
Parameters
dataFormat
SnapObjects.Data.DataFromat
The format of the imported data.
Returns
SnapObjects.Data.IDataImporter
The DataTable data importer.
Examples
The following code example demonstrates how to get DataStore importer in specified format.
using DWNet.Data;
using SnapObjects.Data;
using System;
using System.Data;
using System.IO;
namespace Appeon.ApiDoc.DwDataTableExtensionsExamples
{
public class GetDataImporterExample
{
private readonly SchoolContext _context;
public GetDataImporterExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example()
{
var table = new DataTable();
// Get DataTable importer in JSON format
var importer = table.GetDataImporter(DataFormat.Json);
// Gets the JSON file
string path = Path.Combine(AppContext.BaseDirectory,
@"Jsons\DwDataTableExtensions\ExportJson1.json");
string json = File.ReadAllText(path);
int row = importer.Import(json);
Console.WriteLine("Row: {0}; DataTable Row: {1}; DataTable Column: {2};",
row, table.Rows.Count, table.Columns.Count);
// Output the first row.
// DepartmentID=1; Name=Engineering; Budget=10000;
// StartDate=2020-04-02 09:35:19; Administrator=2;
var firstRow = table.Rows[0];
Console.WriteLine("DepartmentID: {0}; Name: {1}; Budget: {2}; " +
"StartDate: {3}; Administrator: {4}",
firstRow[0].ToString(), firstRow[1].ToString(),
firstRow[2].ToString(), firstRow[3].ToString(),
firstRow[4].ToString());
// Import only the first column of the first row.
row = importer.Import(json, 0, 0, 0, 0);
Console.WriteLine("Row: {0}; DataTable Row: {1}; DataTable Column: {2};",
row, table.Rows.Count, table.Columns.Count);
// Output the second row.
// DepartmentID=1; Name=""; Budget=""; StartDate=""; Administrator="";
var secondRow = table.Rows[1];
Console.WriteLine("DepartmentID: {0}; Name: {1}; Budget: {2}; " +
"StartDate: {3}; Administrator: {4}",
secondRow[0].ToString(), secondRow[1].ToString(),
secondRow[2].ToString(), secondRow[3].ToString(),
secondRow[4].ToString());
// Re-instantiate DataTable and Importer.
table = new DataTable();
importer = table.GetDataImporter(DataFormat.Json);
Console.WriteLine("DataTable Row: {0}; DataTable Column: {1}",
table.Rows.Count, table.Columns.Count);
// Gets the new JSON file
// Only the DepartmentId column in the file.
path = Path.Combine(AppContext.BaseDirectory,
@"Jsons\DwDataTableExtensions\ExportJson2.json");
json = File.ReadAllText(path);
// Import JSON string into DataTable based on key name.
row = importer.ImportByKey(json);
Console.WriteLine("Row: {0}; DataTable Row: {1}; DataTable Column: {2};",
row, table.Rows.Count, table.Columns.Count);
/*This code produces the following output:
Row: 1; DataTable Row: 1; DataTable Column: 5;
DepartmentID: 1; Name: Engineering; Budget: 10000.0000;
StartDate: 2020-04-02T09:35:19; Administrator: 2;
Row: 2; DataTable Row: 2; DataTable Column: 5;
DepartmentID: 1; Name: ; Budget: ;
StartDate: ; Administrator: ;
DataTable Row: 0; DataTable Column: 0;
Row: 1; DataTable Row: 1; DataTable Column: 1;
*/
}
}
}
Example Refer To
JSON Files: ExportJson1 ExportJson2