Sign in Join
Wiki copytemporary

TSQLExporter: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TSQLExporter

OldNewLine
1+**TSQLExporter** ![](/wiki-images/5/56/Tsqlexporter.png) is a database export component for use with a [[TDataSet]]. It may be found on the Data Export tab of the [[Component Palette]] once you have installed the [[lazdbexport]] package. To export the contents of a TDataSet (for instance the result of a [[TSQLQuery]] correctly setup to link with a [[TSQLConnector]] and [[TSQLTransaction]]) also `FormatSettings.TableName` need be provided.
2+
3+In the example below a a [[TSynEdit]] is filled with exported contents of table `mytable`.
4+
5+``` pascal
6+type
7+ myForm = class(TForm)
8+ btnExport: TButton;
9+ myConnector: TSQLConnector;
10+ myExporter: TSQLExporter;
11+ myQuery: TSQLQuery;
12+ myTransaction: TSQLTransaction;
13+ myScriptText: TSynEdit;
14+ mySyntax: TSynSQLSyn;
15+ procedure btnExportClick(Sender: TObject);
16+ private
17+ procedure ExportTable( const tblnam: String );
18+ end;
19+
20+implementation
21+
22+procedure myForm.btnExportClick(Sender: TObject);
23+begin
24+ ExportTable( 'mytable' );
25+end;
26+
27+procedure myForm.ExportTable( const tblnam: string );
28+var
29+ ts: TStringStream;
30+begin
31+ try
32+ ts := TStringStream.Create( '' );
33+ myTransaction.Database := myConnector;
34+ myQuery.Database := myConnector;
35+ myExporter.DataSet := myQuery;
36+
37+ myQuery.SQL.Text := 'SELECT * FROM ' + tblnam;
38+ myQuery.Open();
39+
40+ with myExporter.FormatSettings do begin
41+ TableName := tblnam; // exporter uses tablename in insert statements
42+ DateFormat := 'YYYY-MM-DD'; // SQL date format
43+ DateTimeFormat := 'YYYY-MM-DD hh:mm:ss'; //
44+ end;
45+
46+ myExporter.ExportToStream( ts );
47+ myScriptText.Text := ts.DataString; //show formatted script text
48+
49+ myQuery.Close();
50+ finally
51+ ts.Free();
52+ end;
53+end;
54+```
55+
56+---
57+*From the Free Pascal wiki, page "TSQLExporter", as it stood on 2020-07-18 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
58+Category: Components
59+Category: Databases
60+Category: LCL
61+Category: Lazarus

Back to history