通过数据库连接参数保证程序可移植性
2007-05-19 09:44:21 来源:WEB开发网在程序数据模块的Create事件中添加下面的代码: procedure TDM.DataModuleCreate(Sender: TObject);
var
confForm : TConf;
begin
conn.ConnectionString := RWini.getConnStr;
try
conn.Open;
except
on Err : EOLEException do
begin
confForm := TConf.Create(self);
confForm.ShowModal;
end;
end;
end;
这样,在程序执行过程中,将自动从INI文件中读取数据库连接参数,连接数据库,如果连接出现错误,将显示参数配置窗口,让用户配置数据库连接参数。配置窗口的代码如下: //上面的代码省略
uses RWini;
{$R *.dfm}
procedure TConf.FormShow(Sender: TObject);
var
sServer,sDatabase,sUser,sPassword : String;
begin
readParam(sServer,sDatabase,sUser,sPassword);
self.edtServer.Text := sServer;
self.edtDatabase.Text := sDatabase;
self.edtUser.Text := sUser;
self.edtPassword.Text := sPassword;
self.ActiveControl := self.btnOK;
end;
procedure TConf.btnCancelClick(Sender: TObject);
begin
close;
end;
procedure TConf.btnOKClick(Sender: TObject);
var
sServer,sDatabase,sUser,sPassword : String;
begin
sServer := self.edtServer.Text;
sDatabase := self.edtDatabase.Text;
sUser := self.edtUser.Text;
sPassword := self.edtPassword.Text;
writeParam(sServer,sDatabase,sUser,sPassword);
close;
end;
更多精彩
赞助商链接