我有一个在项目之间共享的SQLConnection,在runtine中它从ini文件中读取配置,是否有某种方法可以在设计时加载相同的配置?
答案 0 :(得分:3)
一种方法是编写自己的TSQLConnection后代。
答案 1 :(得分:3)
你必须为它创建自己的自定义组件,我们称之为TCustomSQLConnection。只需将此组件放在表单或数据模块上,将一个名为ConfigurationFile的自定义属性设置为您的ini文件,就可以了。如果我理解正确,这就是你想要的 - 如果没有我的意见。
请查看以下代码
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.
您只需在添加自己的加载配置的代码后安装此组件,就可以了。
我会将此组件放在数据模块上,以及项目之间共享的一些其他组件。
希望它有所帮助。
答案 2 :(得分:0)
我假设您使用的是Delphi 2009或2010.您可以先参考我的博客文章:http://chee-yang.blogspot.com/2008/09/delphi-2009-using-dbx4-framework.html
我已经跟踪了这个问题很长一段时间了。在文章中,提出了相当多的质量控制报告。有些已经在Delphi 2010中解决了。请先看看,我们可以在后期讨论。