5,388
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.ODBC,
FireDAC.Phys.ODBCDef, FireDAC.VCLUI.Wait, FireDAC.Phys.MSSQL,
FireDAC.Phys.MSSQLDef, Vcl.StdCtrls, FireDAC.Phys.ODBCBase, Data.DB,
FireDAC.Comp.Client, FireDAC.Comp.BatchMove, FireDAC.Comp.BatchMove.SQL,
FireDAC.Comp.BatchMove.DataSet;
type
TForm1 = class(TForm)
FDBatchMoveSQLReader: TFDBatchMoveSQLReader;
FDBatchMoveSQLWriter: TFDBatchMoveSQLWriter;
FDBatchMove: TFDBatchMove;
FDConnectionExcel: TFDConnection;
FDConnectionSQL: TFDConnection;
FDPhysMSSQLDriverLink: TFDPhysMSSQLDriverLink;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
FDBatchMove.GuessFormat;
FDBatchMove.Execute;
end;
end.
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 214
ClientWidth = 421
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 168
Top = 160
Width = 75
Height = 25
Caption = #26356#26032
TabOrder = 0
OnClick = Button1Click
end
object FDBatchMoveSQLReader: TFDBatchMoveSQLReader
Connection = FDConnectionExcel
ReadSQL = 'select a, c as b from [Sheet1$]'
Left = 53
Top = 136
end
object FDBatchMoveSQLWriter: TFDBatchMoveSQLWriter
Connection = FDConnectionSQL
TableName = 'Test'
Left = 343
Top = 136
end
object FDBatchMove: TFDBatchMove
Reader = FDBatchMoveSQLReader
Writer = FDBatchMoveSQLWriter
Mode = dmUpdate
Mappings = <>
LogFileName = 'Data.log'
Left = 183
Top = 24
end
object FDConnectionExcel: TFDConnection
Params.Strings = (
'ODBCDriver=Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb' +
')'
'DataSource=Excel Files'
'Database=D:\Test.xlsx'
'User_Name=admin'
'ODBCAdvanced=DefaultDir=D:\;DriverId=1046;MaxBufferSize=2048;Max' +
'ScanRows=8;PageTimeout=5;ReadOnly=1;SafeTransactions=0;Threads=3' +
';UserCommitSync=Yes'
'DriverID=ODBC')
LoginPrompt = False
Left = 53
Top = 80
end
object FDConnectionSQL: TFDConnection
Params.Strings = (
'DriverID=MSSQL'
'OSAuthent=Yes'
'Server=LocalHost'
'Database=TestDB')
Connected = True
LoginPrompt = False
Left = 343
Top = 80
end
object FDPhysMSSQLDriverLink: TFDPhysMSSQLDriverLink
Left = 343
Top = 24
end
end
USE TestDB;
GO
DROP TABLE Test
CREATE TABLE Test (a INTEGER PRIMARY KEY, b NVARCHAR(16)) --必须有primary key
INSERT INTO Test VALUES (1, '实例'), (2, '看看')
SELECT * FROM Test