Importing & Exporting Records: A Guide

Estimated reading time: 2 minutes

The following code fragments show the use of the Export Class to write to a text file the records of a file’s member using a comma-separated format. There is also an example of using the Import Class to populate another member with the comma-separated data generated by Export.

public class CustomsAgent
{
    AdgConnection dbConnection;

    const string CsvFilePath = "C:/Temp/Dealer/ItemList.csv";

    . . .

    void LogMe(object sender, OpMessageArgs args)
    {
        Console.WriteLine(args.Message);
    }

    void ExportFile()
    {
        IMember sourceMember = AdgFactory.NewMember(dbConnection, "/MYLIB/ITEMMASTER/ITEMMASTER");
        int recs = (int)sourceMember.ActiveRecords;
        Console.WriteLine("Exporting {0} with {1} Records", sourceMember, recs);

        ExportToCsvOptions exportOptions = new ExportToCsvOptions(sourceMember, CsvFilePath, ExportAccessMode.Key);
        exportOptions.LogMessageEvent += LogMe;

        Task exportTask = Export.With(exportOptions);
        exportTask.RunSynchronously();

        Console.WriteLine("Finished exporting to {0} with status of: {1}", CsvFilePath, exportTask.Status);
    }

    void ImportFile()
    {
        IMember importMember = AdgFactory.NewMember(dbConnection, "/MYLIB/ITEM_EMPTY/ITEM_EMPTY");
        importMember.Clear();

        ImportFromCsvOptions importOptions = new ImportFromCsvOptions(importMember, CsvFilePath);
        importOptions.LogMessageEvent += LogMe;

        Task importTask = Import.With(importOptions);
        importTask.RunSynchronously();
    }

    public void Work()
    {
        dbConnection.Open();

        ExportFile();
        ImportFile();

        dbConnection.Close();
    }

    . . .

}

It is also possible to use XML format instead of Comma Sparated Values to export and import data, to do so, use the ImportOptions and ExportOptions classes instead of the ImportFromCsvOptions or ExportToCsvOptions classes.

See Also

Import Class

Export Class