Data Link — CSV
TTMSFNCDataLinkCSV loads CSV data into a TTMSFNCDataSet, enabling standard dataset operations (browse, filter, sort, bind) on CSV files without a database.
Key class: TTMSFNCDataLinkCSV
Loading a CSV File
Drop TTMSFNCDataLinkCSV on the form and either:
- Set the
FileNameproperty at design time — the file loads automatically. - Call
LoadFromFileorLoadFromStreamat runtime.
TMSFNCDataLinkCSV1.FileName := 'data.csv';
// or
TMSFNCDataLinkCSV1.LoadFromFile('data.csv', TEncoding.UTF8);
TMSFNCDataLinkCSV1.LoadFromStream(MyStream, TEncoding.UTF8);
TMSFNCDataLinkCSV1->FileName = "data.csv";
// or
TMSFNCDataLinkCSV1->LoadFromFile("data.csv", TEncoding::UTF8);
TMSFNCDataLinkCSV1->LoadFromStream(MyStream, TEncoding::UTF8);
Link to a TTMSFNCDataSet:
TMSFNCDataSet1.DataLink := TMSFNCDataLinkCSV1;
TMSFNCDataSet1.Active := True;
TMSFNCDataSet1->DataLink = TMSFNCDataLinkCSV1;
TMSFNCDataSet1->Active = true;
Separator Detection
| Property | Description |
|---|---|
AutoSeparator |
When True, auto-detects the field separator (comma, semicolon, tab, etc.) |
Separator |
Delimiter character used when AutoSeparator is False |
HasFieldNames |
When True, the first row is treated as a header row with field names |
Automatic Saving
Set AutoSaveChanges := True to persist modifications back to the file as they occur, without explicit save calls.
Combining Separator Configuration, Loading, and Saving
Configure the parser before loading the file, connect the data link to the dataset, and enable automatic saving only when every dataset edit should be written back immediately:
begin
TMSFNCDataSet1.Close;
TMSFNCDataSet1.DataLink := TMSFNCDataLinkCSV1;
TMSFNCDataLinkCSV1.AutoSeparator := False;
TMSFNCDataLinkCSV1.Separator := ';';
TMSFNCDataLinkCSV1.HasFieldNames := True;
TMSFNCDataLinkCSV1.FileEncoding := feUTF8;
TMSFNCDataLinkCSV1.AutoSaveChanges := True;
TMSFNCDataLinkCSV1.LoadFromFile('customers.csv', TEncoding.UTF8);
TMSFNCDataSet1.Open;
end;
TMSFNCDataSet1->Close();
TMSFNCDataSet1->DataLink = TMSFNCDataLinkCSV1;
TMSFNCDataLinkCSV1->AutoSeparator = false;
TMSFNCDataLinkCSV1->Separator = L';';
TMSFNCDataLinkCSV1->HasFieldNames = true;
TMSFNCDataLinkCSV1->FileEncoding = Tms::Tmsfnccustomdatalink::TTMSFNCCustomDataLinkFileEncoding::feUTF8;
TMSFNCDataLinkCSV1->AutoSaveChanges = true;
TMSFNCDataLinkCSV1->LoadFromFile("customers.csv", TEncoding::UTF8);
TMSFNCDataSet1->Open();
Common Pitfalls
- Set
AutoSeparator,Separator, andHasFieldNamesbeforeLoadFromFile; changing them after the load does not reinterpret rows already in memory. AutoSaveChangeswrites each edit back toFileName. Leave it disabled for read-only imports or when several edits must be reviewed before one explicitSaveToFilecall.- Keep
FileEncodingaligned with the encoding passed toLoadFromFile. Automatic saving usesFileEncodingwhen it writes the file again. - Activate the dataset only after assigning its
DataLinkand loading the source, so field definitions are built from the intended CSV structure.
Methods
| Method | Description |
|---|---|
LoadFromFile(AFileName, AEncoding) |
Load from a file with the specified encoding |
LoadFromStream(AStream, AEncoding) |
Load from a stream |
SaveToFile(AFileName, AEncoding) |
Save to a file |
SaveToStream(AStream, AEncoding) |
Save to a stream |
Properties
| Property | Description |
|---|---|
AutoSaveChanges |
Auto-persist changes (default: False) |
AutoSeparator |
Auto-detect field separator |
FileEncoding |
Encoding for file operations |
FileName |
CSV file path |
HasFieldNames |
First row contains field names |
QuoteEmptyValues |
Enclose empty values in quotes when saving |
Separator |
Field delimiter when AutoSeparator is False |