tags:

views:

270

answers:

3

I have a SQLConnection which is shared between projects, in runtine it reads the configuration from an ini file, is there some way to load this same configuration in design time?

+3  A: 

One way is to write your own TSQLConnection descendent.

Erick Sasse
A: 

I assume you are using Delphi 2009 or 2010. You may refer to my blog article first: http://chee-yang.blogspot.com/2008/09/delphi-2009-using-dbx4-framework.html

I have track this problem for quite some time. In the article, there are quite a number of QC report raised. Some already solved in Delphi 2010. Please have a look first and we may discuss in later stage.

Chau Chee Yang
I think you didn't understand my question. I want to load the settings in design time, the only way I can connect to a Database in design time is by configuring the connection in the dfm, and I don't want to do that.
Fabio Gomes
+2  A: 

You have to create your own custom component for it, let's call it TCustomSQLConnection. Just drop this component on the form or on a datamodule, set up a custom property called ConfigurationFile to your ini file, and you're good to go. If I understand correctly, this is what you want - my appologies if not.

Please take a look at the following code,

unit uSQLCustomConnection;

interface

uses
  SysUtils, Classes, DB, SqlExpr;

type
  TCustomSQLConnection = class(TSQLConnection)
  private
    FConfigurationFile  : String;
    procedure SetConfigurationFile(Value: TStrings);
    procedure LoadConfiguration(AConfigurationFile: String);
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
  published
    property ConfigurationFile : String read FConfigurationFile write SetConfigurationFile;
  end;

procedure Register;

implementation

constructor TCustomSQLConnection.Create(AOwner: TComponent);
begin
  inherited;
  FConfigurationFile := '';
end;

destructor TCustomSQLConnection.Destroy;
begin
// free memory if needed
  inherited;
end;

procedure TCustomSQLConnection.SetConfigurationFile(Value: String);
begin
  FConfigurationFile := Value;
  if FileExists(FConfigurationFile) then
    LoadConfiguration(FConfigurationFile);
end;

procedure TCustomSQLConnection.LoadConfiguration(AConfigurationFile: String);
begin
// Put the code that loads the configuration here
end;

procedure Register;
begin
  RegisterComponents('Samples', [TCustomSQLConnection]);
end;

end.

All you have to do is to install this component after adding your own code that loads the configuration, and you're good to go.

I would drop this component on a Data Module, alongside with some other components that are shared between projects.

Hope it helps.

Dan S