lphi 5.0的注册表控件 Delphi的TRegistry提供了操作Windows注册表的界面,可以在程序中读写注册表中的数据。如果不使用InstallShield,通过TRegistry也可以完成DSN的初始设置。本文仅介绍利用TRegistry实现对DSN的修改,而不介绍DSN的完整设置,但根据上述原理和下面的程序代码,要做到这一点不会有任何问题。
三、程序代码 仍然以CenterAuto数据源为例,假设应用程序中有一个“系统设置”选项,允许用户改变CenterAuto数据源连接的Auto.mdb的存放路径,范例程序如下: 1、“系统设置”Form
2、代码Option.pas unit Option; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Registry; type TOptionForm = class(TForm) lbPrompt: TLabel; edtAutoPath: TEdit; btnOK: TButton; btnCancel: TButton; OpenDialog: TOpenDialog; btnBrowse: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure btnBrowseClick(Sender: TObject); procedure btnOKClick(Sender: TObject); private { Private declarations } SystemReg:TRegistry; //TRegistry在Registry单元中定义 public { Public declarations } end; implementation procedure TOptionForm.FormCreate(Sender: TObject); begin SystemReg:=TRegistry.Create; with SystemReg do begin //TRegistry创建后,默认的RootKey是HKEY_CURRENT_USER RootKey:=HKEY_LOCAL_MACHINE; //打开CenterAuto主键 OpenKey('Software/ODBC/ODBC.INI/CenterAuto',False) //读取原来的数据库(含路径) edtAutoPath.Text:=ReadString('DBQ') end; end; procedure TOptionForm.FormDestroy(Sender: TObject); begin with SystemReg do begin //关闭打开的主键 CloseKey; //释放SystemReg Free; end; end; procedure TOptionForm.btnBrowseClick(Sender: TObject); begin if OpenDialog.Execute then //获取路径和文件名 edtAutoPath.Text:=OpenDialog.FileName; end; procedure TOptionForm.btnOKClick(Sender: TObject); begin //设置新的数据库和路径 SystemReg.WriteString('DBQ',edtAutoPath.Text); end; end. 3、使用系统设置Form 在程序中,通过下列代码就可以实现ODBC数据源的动态设置了: with ToptionForm.Create do begin ShowModal; Release; end; 四、小结 本文通过对ODBC数据源和Delphi 5.0种系统注册表的分析,给出了在程序中动态设置ODBC数据源的方法,提高了程序安装、设置的灵活性。实践中,应用于《杭州市交警支队违章数据中心管理系统》,收到了良好的效果。 参考文献 [1]Steve Teixeira,《Delphi 4开发大全》,人民邮电出版社,1999年
上一页 [1] [2]
Tags:
|