在运行时设置 Firedac fdquery 和 fdconnection
Setup a Firedac fdquery and fdconnection in runtime
我需要在运行时 setup/run fdconnection 和 fdquery。但是怎么办?
我正在我的代码中尝试这样做:
var
LocalConnection: TFDConnection;
LicencasTable: TFDQuery;
FDPhysMySQLDriverLink1: TFDPhysMySQLDriverLink;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
oParams: TStrings;
begin
LocalConnection := TFDConnection.Create(Self);
FDPhysMySQLDriverLink1 := TFDPhysMySQLDriverLink.Create(Self);
FDGUIxWaitCursor1 := TFDGUIxWaitCursor.Create(Self);
FDPhysMySQLDriverLink1.VendorLib := ExtractFilePath(Application.exename)+'libmysql.dll';
try
if Variaveis.hostbd <> '' then begin
oParams := TStringList.Create;
oParams.Add('Server=' + Variaveis.hostbd);
oParams.Add('Database='+Variaveis.databasebd);
oParams.Add('User_Name='+Variaveis.usuariobd);
oParams.Add('Password='+Variaveis.password);
oParams.Add('DriverID=MySQL');
LocalConnection.Params.UserName := Variaveis.usuariobd;
LocalConnection.Params.Password := Variaveis.password;
FDManager.AddConnectionDef('bloqueio', 'MySQL', oParams);
LocalConnection.DriverName := 'MySQL';
LocalConnection.LoginPrompt := false;
LocalConnection.Connected := true;
if LocalConnection.Connected then
// Busca o registro do sistema no bd
LicencasTable := TFDQuery.Create(Self);
LicencasTable.Connection := LocalConnection;
ShowMessage(Pchar(LicencasTable.Connection));
LicencasTable.Close;
LicencasTable.SQL.Clear;
ShowMessage('Before open');
LicencasTable.Open('select * from licencas');
ShowMessage('After open');
LicencasTable.Active := true;
if LicencasTable.Active then begin
ShowMessage('Active');
end;
if not LicencasTable.Active then begin
ShowMessage('This is not Active');
end;
end;
except
oParams.Free;
end;
一切正常,直到到达“LicencasTable.Open('select * from licencas');”
query.open 根本不起作用,也没有给我任何错误。为什么打不开,也不给我报错?
我忘了做什么?
您的代码在几个方面是不正确的。首先,您不必关闭它或清除新创建的查询上的 SQL。其次,您通常不会使用 Open
方法来设置 SQL 语句。第三,您使用 try..except
来释放资源,这是不正确的;如果没有异常发生,则说明存在内存泄漏。您应该改用 try..finally
。
try
if Variaveis.hostbd <> '' then begin
oParams := TStringList.Create;
oParams.Add('Server=' + Variaveis.hostbd);
oParams.Add('Database='+Variaveis.databasebd);
oParams.Add('User_Name='+Variaveis.usuariobd);
oParams.Add('Password='+Variaveis.password);
oParams.Add('DriverID=MySQL');
LocalConnection.Params.UserName := Variaveis.usuariobd;
LocalConnection.Params.Password := Variaveis.password;
FDManager.AddConnectionDef('bloqueio', 'MySQL', oParams);
LocalConnection.DriverName := 'MySQL';
LocalConnection.LoginPrompt := false;
LocalConnection.Connected := true;
if LocalConnection.Connected then
begin
// Busca o registro do sistema no bd
LicencasTable := TFDQuery.Create(Self);
LicencasTable.Connection := LocalConnection;
ShowMessage(Pchar(LicencasTable.Connection));
ShowMessage('Before open');
LicencasTable.SQL.Text := 'select * from licencas';
LicencasTable.Active := true;
if LicencasTable.Active then begin
ShowMessage('Active');
end;
if not LicencasTable.Active then begin
ShowMessage('This is not Active');
end;
end;
finally
oParams.Free;
end;
我使用这个代码:
procedure ConnectLiveTest( fdc: String );
var i: Integer;
begin
dm.fdc.Connected:= False;
for i := 0 to dm.ComponentCount -1 do
if dm.Components[ i ] is TFDQuery then
begin
if fdc = 'Live' then
begin
with dm.fdc.Params as TFDPhysMSSQLConnectionDefParams do
begin
Server := 'server ip address';
Database:= 'dbname';
UserName:= 'username';
Password:= 'password';
end;
end
else
begin
with dm.fdc.Params as TFDPhysMSSQLConnectionDefParams do
begin
Server := 'alt ip address';
Database:= 'Testdbname';
UserName:= 'username';
Password:= 'password';
end;
end;
dm.fdc.Connected:= True;
TFDQuery( dm.Components[ i ]).Active:= True;
end;
end;
所以我只使用一个 Firedac 连接 ('fdc'),当我激活主窗体时,我将 'Live' 或 'Test' 传递给连接到所需服务器的上述过程.
我需要在运行时 setup/run fdconnection 和 fdquery。但是怎么办?
我正在我的代码中尝试这样做:
var
LocalConnection: TFDConnection;
LicencasTable: TFDQuery;
FDPhysMySQLDriverLink1: TFDPhysMySQLDriverLink;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
oParams: TStrings;
begin
LocalConnection := TFDConnection.Create(Self);
FDPhysMySQLDriverLink1 := TFDPhysMySQLDriverLink.Create(Self);
FDGUIxWaitCursor1 := TFDGUIxWaitCursor.Create(Self);
FDPhysMySQLDriverLink1.VendorLib := ExtractFilePath(Application.exename)+'libmysql.dll';
try
if Variaveis.hostbd <> '' then begin
oParams := TStringList.Create;
oParams.Add('Server=' + Variaveis.hostbd);
oParams.Add('Database='+Variaveis.databasebd);
oParams.Add('User_Name='+Variaveis.usuariobd);
oParams.Add('Password='+Variaveis.password);
oParams.Add('DriverID=MySQL');
LocalConnection.Params.UserName := Variaveis.usuariobd;
LocalConnection.Params.Password := Variaveis.password;
FDManager.AddConnectionDef('bloqueio', 'MySQL', oParams);
LocalConnection.DriverName := 'MySQL';
LocalConnection.LoginPrompt := false;
LocalConnection.Connected := true;
if LocalConnection.Connected then
// Busca o registro do sistema no bd
LicencasTable := TFDQuery.Create(Self);
LicencasTable.Connection := LocalConnection;
ShowMessage(Pchar(LicencasTable.Connection));
LicencasTable.Close;
LicencasTable.SQL.Clear;
ShowMessage('Before open');
LicencasTable.Open('select * from licencas');
ShowMessage('After open');
LicencasTable.Active := true;
if LicencasTable.Active then begin
ShowMessage('Active');
end;
if not LicencasTable.Active then begin
ShowMessage('This is not Active');
end;
end;
except
oParams.Free;
end;
一切正常,直到到达“LicencasTable.Open('select * from licencas');”
query.open 根本不起作用,也没有给我任何错误。为什么打不开,也不给我报错?
我忘了做什么?
您的代码在几个方面是不正确的。首先,您不必关闭它或清除新创建的查询上的 SQL。其次,您通常不会使用 Open
方法来设置 SQL 语句。第三,您使用 try..except
来释放资源,这是不正确的;如果没有异常发生,则说明存在内存泄漏。您应该改用 try..finally
。
try
if Variaveis.hostbd <> '' then begin
oParams := TStringList.Create;
oParams.Add('Server=' + Variaveis.hostbd);
oParams.Add('Database='+Variaveis.databasebd);
oParams.Add('User_Name='+Variaveis.usuariobd);
oParams.Add('Password='+Variaveis.password);
oParams.Add('DriverID=MySQL');
LocalConnection.Params.UserName := Variaveis.usuariobd;
LocalConnection.Params.Password := Variaveis.password;
FDManager.AddConnectionDef('bloqueio', 'MySQL', oParams);
LocalConnection.DriverName := 'MySQL';
LocalConnection.LoginPrompt := false;
LocalConnection.Connected := true;
if LocalConnection.Connected then
begin
// Busca o registro do sistema no bd
LicencasTable := TFDQuery.Create(Self);
LicencasTable.Connection := LocalConnection;
ShowMessage(Pchar(LicencasTable.Connection));
ShowMessage('Before open');
LicencasTable.SQL.Text := 'select * from licencas';
LicencasTable.Active := true;
if LicencasTable.Active then begin
ShowMessage('Active');
end;
if not LicencasTable.Active then begin
ShowMessage('This is not Active');
end;
end;
finally
oParams.Free;
end;
我使用这个代码:
procedure ConnectLiveTest( fdc: String );
var i: Integer;
begin
dm.fdc.Connected:= False;
for i := 0 to dm.ComponentCount -1 do
if dm.Components[ i ] is TFDQuery then
begin
if fdc = 'Live' then
begin
with dm.fdc.Params as TFDPhysMSSQLConnectionDefParams do
begin
Server := 'server ip address';
Database:= 'dbname';
UserName:= 'username';
Password:= 'password';
end;
end
else
begin
with dm.fdc.Params as TFDPhysMSSQLConnectionDefParams do
begin
Server := 'alt ip address';
Database:= 'Testdbname';
UserName:= 'username';
Password:= 'password';
end;
end;
dm.fdc.Connected:= True;
TFDQuery( dm.Components[ i ]).Active:= True;
end;
end;
所以我只使用一个 Firedac 连接 ('fdc'),当我激活主窗体时,我将 'Live' 或 'Test' 传递给连接到所需服务器的上述过程.