Unit Unit1; Interface uses Winapi.windows, Winapi.messages, System.sysutils, System.variants, system.classes, Vcl.graphics, Vcl.C
Ontrols, Vcl.forms, Vcl.dialogs, Vcl.stdctrls, data.db, datasnap.dbclient;
Type TFORM1 = Class (Tform) Cds:tclientdataset;
Button1:tbutton;
Button2:tbutton;
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Private {Private Declarations} procedure ExportData (const tablenames:string);
Procedure ImportData (const fileName, tablenames:string);
Public {public declarations} end;
var Form1:tform1;
Implementation {$R *.DFM} uses untdb;
Procedure Tform1.button1click (Sender:tobject);
Begin ExportData (' bas_kind,bas_goods ');
End
Procedure Tform1.button2click (Sender:tobject);
Begin ImportData (Extractfilepath (application.exename) + ' export.sql ', ' bas_kind,bas_goods ');
End
Procedure Tform1.exportdata (const tablenames:string);
var sql, err:string;
SL, Tables:tstringlist; I, H:Integer;
FieldNames, values:string;
function GetValue (Field:tfield): string; The Begin case field. DataType of ftstring, fttimestamp:begin result: = Quotedstr (field.
asstring);
End Ftboolean:begin case field.
Asboolean of True:result: = ' 1 ';
False:result: = ' 0 ';
End
End else if not varisnull (field. Value) THEN BEGIN result: = Vartostr (field.
Value);
End Else Result: = ' null ';
End
End
Begin IF Tablenames = ' then exit;
Tables: = Tstringlist.create; Tables.
Delimiter: = ', '; Tables.
Delimitedtext: = Tablenames;
SL: = Tstringlist.create; Sl.
Clear; For h: = 0 to tables.
Count-1 do begin sql: = ' select * from ' + tables[h]; If Frmdb.querysql (SQL, CDs, ERR) and (not CDs). ISEMPTY) THEN BEGIN CDs.
A; While not CDs.
Eof do begin fieldnames: = ';
Values: = '; For I: = 0 to CDs. FieldCount-1 do BEGIN if FieldNames = ' THEN BEGIN fieldnames: = CDs. Fields[i].
FieldName; End ELSE begin FieldNames: = FieldNames + ', ' + CDs. Fields[i].
FieldName;
End If values = ' THEN BEGIN values: = GetvaLue (CDS).
Fields[i]); End ELSE BEGIN values: = values + ', ' + GetValue (CDS).
Fields[i]);
End
End Sl.
ADD (' insert INTO ' + tables[h] + ' (' + FieldNames + ') VALUES (' + values + ') '); Cds.
Next;
End
End
End Sl.
SaveToFile (Extractfilepath (application.exename) + ' export.sql '); Sl.
Free; Tables.
Free;
End
Procedure Tform1.importdata (const fileName, tablenames:string);
var cmd, tables:tstringlist;
Err, sql:string;
I:integer;
Begin if (not FileExists (fileName)) or (Tablenames = ') then exit;
Tables: = Tstringlist.create; Tables.
Delimiter: = ', '; Tables.
Delimitedtext: = Tablenames; For I: = 0 to tables.
Count-1 do begin sql: = ' truncate table ' + tables[i];
Frmdb.executesql (SQL, err);
End Tables.
Free;
CMD: = tstringlist.create; Cmd.
LoadFromFile (FileName); Frmdb.executesql (cmd.
Text, err); Cmd.
Free;
End End.
Author:cnblogs Yong South Delphi
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Delphi/