DELPHI XE 10.1 BERLIN uses FIREDAC to connect to the database (SQL SERVER, MYSQL, SQLITE)

Posted by TRemmie on Sat, 22 Jan 2022 13:56:24 +0100

This program is a Demo written, including a main form and a data module

1. The main window is designed as follows:

Provide an initialization, a query, and a release

Initialization is used for data module instantiation. Query is to execute SQL statements, and release is to release data module instances Nothing else to say

At the top of the form is a RadioGroup. Four buttons are missing, that is, four database types. The TAG of each button control is set with 0123 respectively, which is used to switch the database connection switching mode

At the bottom left of the form are four SQL statements corresponding to four databases

2. The data module is designed as follows:

First, create a new Notepad under the program root directory and rename it driver Ini, as follows:

[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

It can be seen that there are three connection methods, namely MSSQL_Pool, MySQL_Pool,SQLite_Pool, which corresponds to three kinds of database connections, is user - defined

If you don't know how these parameters come from, you can drop a TFDConnection control in the data module (delete it at last), and double-click the control, as shown in the following figure:

Then configure the parameters, and then click [Info], as shown below:

The selected part in the figure above is actually driver Ini, copy it and customize a node (such as MSSQL_Pool)

Then we'll talk about the controls in the data module:

Tfdmmanager sets the property ConnectionDefFileName, which is driver Ini path is OK, because driver Ini is in the root directory, so write Driver.ini directly Ini. Set fetchoptions The mode is fmAll

TFDConnection setting fetchoptions The mode is fmall Connectiondefname is specified dynamically

Set the connection property of TFDQuery and select TFDConnection from the drop-down list

TFDGUIxWaitCursor lost one

TFDPhysMSSQLDriverLink lost a connection driver

TFDPhysMySQLDriverLink lost a connection driver

TFDPhysSQLiteDriverLink lost a connection driver

TFDPhysOracleDriverLink lost a connection driver

3. The main window code is as follows:

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;   //Get a connection in the connection pool
  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;  //The connection is not disconnected
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('Data module not initialized!');
    Exit;
  end;
  if DM.DBChanged(TRadioButton(Sender).Tag) then
    ShowMessage('Database connection switching succeeded!')
  else
    ShowMessage('Database connection switching failed!');
end;
 
end.

4. Data module code is as follows:

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 default 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.

It should be noted that when connecting to MySQL, you also need two dll drivers, which are in the source code connection

Since Oracle is not installed on this computer, there is no connection to write oracle

Topics: Delphi