Component for exporting a data set to a CSV file
The following step-by-step instructions are for creating a .NET component that exports a set of data to a CSV file. This is an example of how to create and use the aggregate protocol.
For simplicity, the .NET code assumes that data received does not contain a comma.
The component expects data from the following columns of the SCHEDTOUR table: SCHEDTOUR_ID, DESTINATION, TOUR_TYPE, START_DATE, RETURN_DATE, PRICE.
Creating a .NET component for exporting records to a CSV file
1. Choose Define, RDMI, dotNet Components from the USoft Definer menu. The DotNetComponents window opens.
2. Provide a Name for your new component, in this case: CSV.
3. In the Program Source field, provide the source code of the component:
using System;
using System.Text;
using System.IO;
class CSV
{
string csvFileName = null;
StringBuilder csv = new StringBuilder();
public long fileExportExe(Nullable<Int32> id, string destination, string tourType, Nullable<DateTime> startDate, Nullable<DateTime> returnDate, Nullable<Int32> price, string filename)
{
csvFileName = filename;
string newLine = string.Format("{0},{1},{2},{3},{4},{5}", id, destination, tourType, startDate, returnDate, price);
csv.AppendLine(newLine);
return 1;
}
public long fileExportFetch(out string filename)
{
filename = csvFileName;
File.WriteAllText(filename, csv.ToString());
csv.Clear();
return 1;
}
}
4. Press Commit. USoft compiles this code.
5. Press the Check button. The RDMI interface is generated by USoft.
Defining an aggregate protocol for the component
1. In the DotNet Components window, click the Aggregate Protocol Associations tab page.
2. Fill out the following fields:
-
Protocol = EXPORT
-
Execute Method = FILEEXPORTEXE
-
Fetch Method = FILEEXPORTFETCH
3. Save/commit.
Testing the solution
1. Open your client/server application.
2. In Sql Command, execute:
INVOKE csv.export WITH
SELECT schedtour_id
, destination
, tour_type
, start_date
, return_date
, price
, 'C:\Temp\schedtour.csv'
FROM schedtour
3. See that a file has been created in location:
C:\Temp\schedtour.csv
4. Open the file in an text editor. See that it contains the input data in a comma-separated-values format.