此程序是寫的一個Demo, 包含一個主窗體, 一個數據模塊.
1. 主窗體設計如下:

提供一個初始化, 一個查詢, 一個釋放.
初始化用於數據模塊實例化, 查詢就是執行SQL語句, 釋放就是釋放數據模塊實例. 其它沒啥說的.
窗體上方是一個RadioGroup, 丟了四個按鈕, 也就是四個數據庫類型, 每個按鈕控件的TAG分別設置了0123, 用於數據庫連接切換方式的切換..
窗體左下方是四個SQL語句, 分別對應了四個數據庫.
2. 數據模塊設計如下:

首先, 在程序根目錄下新建一個記事本, 重命名為Driver.ini, 內容如下:
[MSSQL_Pool]
DriverID=MSSQL
Pooled=True
Server=192.168.2.188
Database=yqsh_SmartTable
User_Name=sa
Password=123
POOL_CleanupTimeout=0
POOL_ExpireTimeout=0
POOL_MaximumItems=50
CharacterSet=UTF8
[MySQL_Pool]
DriverID=MySQL
Pooled=True
Database=ungsm
User_Name=root
Password=1234
POOL_CleanupTimeout=0
POOL_ExpireTimeout=0
POOL_MaximumItems=50
Server=192.168.2.188
UseSSL=True
CharacterSet=utf8
[SQLite_Pool]
Pooled=True
Database=smarttable.db
POOL_MaximumItems=10
DriverID=SQLite
可以看出, 有三種連接方式, 分別是MSSQL_Pool, MySQL_Pool,SQLite_Pool, 分別對應三種數據庫連接, 這個名字是自定義的.
如果不知道這些參數怎么來, 可以在數據模塊丟一個TFDConnection控件(最后刪除掉), 雙擊控件, 如下圖所示:

然后配置參數, 然后再點擊[Info], 如下入所示:

上圖選中部分其實就是Driver.ini中的配置參數, 拷貝出來, 自定義一個節點(比如: MSSQL_Pool)就好了.
然后再說下數據模塊中的控件:
TFDManager 設置屬性ConnectionDefFileName, 這個設置Driver.ini的路徑就可以了, 因為Driver.ini就在根目錄下, 就直接寫Driver.ini就可以了, 設置FetchOptions.Mode為fmAll.
TFDConnection 設置FetchOptions.Mode為fmAll. ConnectionDefName動態指定.
TFDQuery 設置connection屬性, 下拉選擇TFDConnection即可.
TFDGUIxWaitCursor 丟一個
TFDPhysMSSQLDriverLink 丟一個 連接驅動
TFDPhysMySQLDriverLink 丟一個 連接驅動
TFDPhysSQLiteDriverLink 丟一個 連接驅動
TFDPhysOracleDriverLink 丟一個 連接驅動
3. 主窗代碼如下:
unit uFrmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
mm1: TMemo;
Button4: TButton;
Button5: TButton;
Button6: TButton;
RadioGroup1: TRadioGroup;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
Label1: TLabel;
Label2: TLabel;
mm2: TMemo;
Label3: TLabel;
mm3: TMemo;
Label4: TLabel;
mm4: TMemo;
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure RadioButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses uDM_DAC;
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
if not Assigned(dm) then
dm:= TDM.Create(nil);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if not Assigned(dm) then
Exit;
if not dm.FDConnection.Connected then
dm.FDConnection.Connected:= True; //在連接池里獲取一個連接
with dm.FDQuery do
begin
Close;
Case DM.FDBLX of
0: SQL.Text:= mm1.Lines.Text;
1: SQL.Text:= mm2.Lines.Text;
2: SQL.Text:= mm3.Lines.Text;
3: SQL.Text:= mm4.Lines.Text;
End;
Open;
end;
ShowMessage(IntToStr(dm.FDQuery.RecordCount));
DM.FDConnection.Connected:= False; //歸還連接 不是斷開
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if Assigned(dm) then
FreeAndNil(dm);
end;
procedure TForm1.RadioButtonClick(Sender: TObject);
begin
if not Assigned(dm) then
begin
ShowMessage('數據模塊未初始化!');
Exit;
end;
if DM.DBChanged(TRadioButton(Sender).Tag) then
ShowMessage('數據庫連接切換成功!')
else
ShowMessage('數據庫連接切換失敗!');
end;
end.
4. 數據模塊代碼如下:
unit uDM_DAC;
interface
uses
SysUtils, Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error,
FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Phys,
FireDAC.Comp.Client, FireDAC.Phys.MSSQLDef, FireDAC.Phys.ODBCBase,
FireDAC.Phys.MSSQL, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, FireDAC.Stan.Pool,
FireDAC.Stan.Async, Data.DB, FireDAC.Stan.Param, FireDAC.DatS,
FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Phys.MySQLDef,
FireDAC.Phys.MySQL, FireDAC.Stan.ExprFuncs, FireDAC.Phys.SQLiteDef,
FireDAC.Phys.SQLite, FireDAC.Phys.OracleDef, FireDAC.Phys.Oracle;
type
TDM = class(TDataModule)
FDManager: TFDManager;
SQLLink: TFDPhysMSSQLDriverLink;
WaitCursor: TFDGUIxWaitCursor;
FDConnection: TFDConnection;
FDQuery: TFDQuery;
MYSQLLINK: TFDPhysMySQLDriverLink;
SQLITELINK: TFDPhysSQLiteDriverLink;
ORACLELINK: TFDPhysOracleDriverLink;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
function GetCode(iType: Integer; var sCode: string): Boolean;
public
{ Public declarations }
//0 Sqlserver 1 Mysql 2 Sqlite 3 Oracle 默認0
FDBLX: Integer;
function DBChanged(iType: integer): Boolean;
end;
var
DM: TDM;
implementation
{$R *.dfm}
procedure TDM.DataModuleCreate(Sender: TObject);
begin
FDBLX:= 0;
FDConnection.ConnectionDefName:= 'MSSQL_Pool';
end;
function TDM.DBChanged(iType: integer): Boolean;
var
sCode: string;
begin
Result:= False;
if iType= FDBLX then
begin
Result:= True;
Exit;
end;
try
if GetCode(iType, sCode) then
begin
FDConnection.ConnectionDefName:= sCode;
FDBLX:= iType;
Result:= True;
end;
except
on e: Exception do
Exit;
end;
end;
function TDM.GetCode(iType: Integer; var sCode: string): Boolean;
begin
Result:= False;
case iType of
0: begin
sCode:= 'MSSQL_Pool';
Result:= True;
end;
1: begin
sCode:= 'MySQL_Pool';
Result:= True;
end;
2: begin
sCode:= 'SQLite_Pool';
Result:= True;
end;
3: begin
sCode:= 'Oracle_Pool';
Result:= True;
end;
else
//
end;
end;
end.
需要注意的是, 連接MySQL的時候, 還需要兩個dll驅動, 在源代碼連接里有.
由於本機沒有安裝Oracle, 就沒有寫Oracle的連接了
