lazarust中SqlConnector的使用
TSQLConnector
是一个多功能的数据库连接器组件,可与任何受支持的数据库一起使用。该组件位于“组件面板”的“SQLdb”选项卡上。
要配置数据库访问,最重要的属性是:
- 连接器类型:
-
TIBConnection Firebird TMSSQLConnection MSSQLServer TMySQL40Connection MySQL 4.0 TMySQL41Connection MySQL 4.1 TMySQL50Connection MySQL 5.0 TMySQL51Connection MySQL 5.1 TMySQL55Connection MySQL 5.5 TMySQL56Connection MySQL 5.6 TMySQL57Connection MySQL 5.7 TMySQL80Connection MySQL 8.0 TODBCConnection ODBC TOracleConnection Oracle TPQConnection Postgres TSQLite3Connection SQLite3 TSybaseConnection Sybase
function CreateSQLConnection(ThisDatabaseLocation: string;
ThisType: DatabaseType; ThisUser: string; ThisPass: string): TSpecialConnector;
begin
Result := TSpecialConnector.Create(nil);
Result.KeepConnection := False;
case ThisType of
dtSQLite:
begin
Result.ConnectorType := 'SQLite3';
if Empty(ThisDatabaseLocation) then
Result.DatabaseName := DefaultDatabase
else Result.DatabaseName := ThisDatabaseLocation;
Result.Params.Add('foreign_keys=ON');
Result.Params.Add('journal_mode=WAL');
end;
dtMSSQL:
begin
Result.ConnectorType := 'MSSQLServer';
if Empty(ThisDatabaseLocation) then
Result.HostName := 'localhost'
else Result.HostName := ThisDatabaseLocation;
Result.DatabaseName := 'DefaultDatabase';
end;
dtPostgreSQL:
begin
Result.ConnectorType := 'PostgreSQL';
if Empty(ThisDatabaseLocation) then
begin
Result.HostName := 'localhost';
Result.DatabaseName := DefaultDatabaseName;
end
else Result.HostName := ThisDatabaseLocation;
end;
dtODBC:
begin
Result.ConnectorType := 'ODBC';
{$IFDEF UNIX}
Result.Params.Add('DRIVER=MDBTools');
{$ELSE}
Result.Params.Add('DRIVER=Microsoft Access Driver (*.mdb, *.accdb)');
{$ENDIF}
Result.Params.Add('DBQ=' + ThisDatabaseLocation);
end;
else
end;
Result.UserName := ThisUser;
Result.Password := ThisPass;
Result.Transaction := TSQLTransaction.Create(Result);
end;