2009-10-21 63 views

回答

3

一种方法是编写您自己的TSQLConnection后代。

0

我假设你正在使用德尔福2009年或2010年。你可以参考我的博客文章第一:http://chee-yang.blogspot.com/2008/09/delphi-2009-using-dbx4-framework.html

我跟踪这个问题相当长的一段时间。文章中提出了很多QC报告。一些已经在德尔福2010年解决。请先看看,我们可能会在后期讨论。

+0

我想你不明白我的问题。我想在设计时加载设置,我可以在设计时连接到数据库的唯一方法是通过在dfm中配置连接,但我不想这样做。 – 2009-10-30 11:50:33

3

你必须为它创建自己的自定义组件,我们称之为TCustomSQLConnection。只需将这个组件放在表单或数据模块上,将一个名为ConfigurationFile的自定义属性设置到您的ini文件中,然后您就可以开始使用了。如果我理解正确,这就是你想要的 - 如果不是我的appologies。

请看看下面的代码,

 
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. 

所有你需要做的就是将自己的代码加载配置后安装该组件,你是好去。

我会把这个组件放在一个数据模块上,以及一些在项目之间共享的其他组件。

希望它有帮助。

相关问题