2009-10-21 71 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. 

所有你需要做的就是將自己的代碼加載配置後安裝該組件,你是好去。

我會把這個組件放在一個數據模塊上,以及一些在項目之間共享的其他組件。

希望它有幫助。

相關問題