我需要在运行时设置/运行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根本不起作用,也没有给我任何错误。为什么不打开,而不显示错误?
我忘了做什么?
答案 0 :(得分:2)
您的代码有两种错误的用法。首先,您不必关闭它或清除新创建的查询上的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;
答案 1 :(得分:1)
我使用以下代码:
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'传递给上述过程,该过程连接到所需的服务器。