Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Set Connection from code


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Mar 4, 2018 1:20 AM Last Post By: Barry Wood
Barry Wood

Posts: 73
Registered: 9/3/01
Set Connection from code  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 1:12 AM
Hi
I have a project that either accesses test data from one SQL database, or live data from another. Currently, to switch from test to live and vice versa I set the tfdConnection connected to False on one, True on the other, then for each tfdQuery I change the Connection to whichever tfdConnection I want then make every query Active. This takes a while and is very manual and I'm sure their must be a way of coding all this.

When I try to code following the above when I try to set the Connection property to the tfdConnection I want I get an error 'Incompatible types: tfdCustomConnection and string'.

Any help on this would be appreciated.
Michael Sawyer ...

Posts: 9
Registered: 9/10/17
Re: Set Connection from code  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 1:29 AM   in response to: Barry Wood in response to: Barry Wood
Can you please provide code where you set the connection property

Barry Wood wrote:
Hi
I have a project that either accesses test data from one SQL database, or live data from another. Currently, to switch from test to live and vice versa I set the tfdConnection connected to False on one, True on the other, then for each tfdQuery I change the Connection to whichever tfdConnection I want then make every query Active. This takes a while and is very manual and I'm sure their must be a way of coding all this.

When I try to code following the above when I try to set the Connection property to the tfdConnection I want I get an error 'Incompatible types: tfdCustomConnection and string'.

Any help on this would be appreciated.
Andreas Schulze

Posts: 4
Registered: 10/30/16
Re: Set Connection from code  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 5:50 AM   in response to: Barry Wood in response to: Barry Wood
Maybe the follwing code snippet helps you:

    if conFireDAC.Connected then begin
      // --- close open datasets here ---
      conFireDAC.Close;
    end;
    //--------------------------------------------------------------------------
    conFireDAC.Params.Clear;
    case integer(cmbboxDatabase.Items.Objects[cmbboxDatabase.ItemIndex]) of
      DBProductive_    : begin
          conFireDAC.Params.Add(Format('Server=%s', [ServerProdcutive_]));
          conFireDAC.Params.Add(Format('Database=%s', [DatabaseProductive_]));
        end;
      DBTest_    : begin
          conFireDAC.Params.Add(Format('Server=%s', [ServerTest_]));
          conFireDAC.Params.Add(Format('Database=%s', [DatabaseTest_]));
        end;
    end;
    conFireDAC.Params.Add(Format('DriverID=%s', [DriverID_]));
    conFireDAC.Params.Add(Format('User_Name=%s', [UserName_]));
    conFireDAC.Params.Add(Format('Password=%s', [Password_]));
    //--------------------------------------------------------------------------
    conFireDAC.LoginPrompt                    := False;
    conFireDAC.ResourceOptions.AutoConnect    := True;
    conFireDAC.ResourceOptions.KeepConnection := True;
    conFireDAC.ConnectionName                 := 'myconnection';
    //--------------------------------------------------------------------------
    try
      conFireDAC.Open;
      //--- open datasets here ----
    except
      on E:Exception do begin
        MessageDlg(E.Message, mtError, [mbOK], 0);
      end;
    end;


on my form i only have a TADConnection, TADPhysIBDriverLink, TADGUIxWaitCursor and the components for the DataSets which i need, for example TADTable or TADQuery...

Barry Wood wrote:
Hi
I have a project that either accesses test data from one SQL database, or live data from another. Currently, to switch from test to live and vice versa I set the tfdConnection connected to False on one, True on the other, then for each tfdQuery I change the Connection to whichever tfdConnection I want then make every query Active. This takes a while and is very manual and I'm sure their must be a way of coding all this.

When I try to code following the above when I try to set the Connection property to the tfdConnection I want I get an error 'Incompatible types: tfdCustomConnection and string'.

Any help on this would be appreciated.

Edited by: Andreas Schulze on Feb 28, 2018 5:52 AM
Pat Ritchey

Posts: 10
Registered: 3/9/99
Re: Set Connection from code  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 9:27 AM   in response to: Barry Wood in response to: Barry Wood
Barry Wood wrote:
When I try to code following the above when I try to set the Connection property to the tfdConnection I want I get an error 'Incompatible types: tfdCustomConnection and string'.
--

That error would indicate that you are trying to assign a string value to a property the is not a string. Probably something like:

   MyQuery.Connection := 'MyConnection';


Since the The TFDQuery's Connection property is not a string, but instead a TFDCustomConnection, the correct assignment statement would be without the quotes:

   MyQuery.Connection := MyConnection;


Having said that, take a look at Andreas' code posted above. There is no reason to have multiple TDFConnection objects. You just need one TFDConnection object and change the parameters of that object as needed.

Pat Ritchey [TeamB Emeritus]

Barry Wood

Posts: 73
Registered: 9/3/01
Re: Set Connection from code  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 4, 2018 1:20 AM   in response to: Pat Ritchey in response to: Pat Ritchey
Many thanks, I've done this now. I have just the one fdConnection and use connection definitions to switch between live and test data. Much simpler than what I was trying to do.

Pat Ritchey wrote:
Barry Wood wrote:
When I try to code following the above when I try to set the Connection property to the tfdConnection I want I get an error 'Incompatible types: tfdCustomConnection and string'.
--

That error would indicate that you are trying to assign a string value to a property the is not a string. Probably something like:

   MyQuery.Connection := 'MyConnection';


Since the The TFDQuery's Connection property is not a string, but instead a TFDCustomConnection, the correct assignment statement would be without the quotes:

   MyQuery.Connection := MyConnection;


Having said that, take a look at Andreas' code posted above. There is no reason to have multiple TDFConnection objects. You just need one TFDConnection object and change the parameters of that object as needed.

Pat Ritchey [TeamB Emeritus]

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02