| 第二十三讲 深入窗体 在PB中使用外部资源——INI文件 | ||||
| 在PB中使用外部资源——INI文件 我们在编写程序过程,有可能会遇到由于外部环境变化,而导致程序无法运行的问题。需要重改变程序,这样才能使用;有时由于用户不同,需要设置不同的参数。这类问题,要求程序员,不能把程序写死,必须写活。解决这类问题有两个方法: 可以在需要变化的程序上,设置一些交互式的对话框,让用户选择输入,这样程序就可以根据用户的输入,分支的去执行,即按用户的输入条件有选择的去执行程序。但这一解决方案存在一个问题,有时这类条件可以不需要用户干预,即没有必要用户去输入条件,而只要管理员设置好了,用户就可以直接使用。这时采用对话框的方式就有些弊端了。那么我们采取第二种方式。 第二种方式,就是我们这一讲要强调的方法:使用PB读取外部资源——INI文件。系统通过读取INI文件,去设置系统运行环境,这对于程序的灵活性是很显著的。 我们很熟悉MicroSoft公司的Windows操作系统。并且经常能看到在Windows操作系统中的windows目录下会有很多的INI文件,比如System.ini文件,Win.ini文件,这些文件都是Windows操作系统的资源文件,它的作用就是让Windows操作系统的一些exe文件用的。 我们讲的PB使用的INI文件的作用就类似Windows操作系统下的INI文件的作用。现在,我们就开始讨论PowerBuilder的外部资源INI文件吧。 [插入video_24_1] [插入video_24_2] 首先,我们来编写一个INI资源文件 打开资源管理器,打开记事本,编写INI文件: [Database] Vendors=ODBC,IN5 I-Net v5.x,SYB SQL Server v4.x,MSS Microsoft SQL Server 6.0 DBMS=MSS Microsoft SQL Server 6.0 database=xhchis serverName=dbstandby DatabasePassword= LogPassword= ServerName= LogId=sa Lock= DbParm=appname='ysh',host='zengni',release='4.2',CursorScroll=100 Prompt=0 将这个文件保存为pb.ini 。 在PB中编程: 首先,建立一个Application对象。(存储为ini.pbl,Application的名称也为ini)。打开Script,选择Open事件。 打开菜单Declare中的Global Variables定义几个全局变量。因为在其他窗口对象中也会用到这些变量。 string Sys_path = "" string profile_name = "pb.ini" string profile = "" 写入程序: string Ls_file_name int Li_hApp Li_hApp = Handle (this, TRUE) if Li_hApp > 0 then Prompt ("系统已经运行") HALT end if profile = Sys_Path + Profile_name if not fileexists(profile) then if GetFileOpenName ("打开文件", profile, Ls_file_name, "*.ini", "初始化文件,*.ini") <> 1 then Prompt ("无法打开初始化文件") halt end if end if SQLCA.DBMS = "MSS Microsoft SQL Server 6.x" SQLCA.Database = "xhchis" SQLCA.ServerName = "dbstandby" SQLCA.LogId = "sa" SQLCA.AutoCommit = False SQLCA.DBParm = "" CONNECT; sqlca.DBMS = ProfileString (profile, "Database", "dbms", " ") sqlca.Database = ProfileString (profile, "Database", "database", " ") sqlca.ServerName = ProfileString (profile, "Database", "servername", " ") sqlca.UserID = ProfileString (profile, "Database", "UserID", " ") sqlca.DbParm = ProfileString (profile, "Database", "DBparm", " ") sqlca.DBPass = ProfileString (profile, "Database", "DatabasePassword", " ") sqlca.Lock = ProfileString (profile, "Database", "lock", " ") sqlca.LogId = ProfileString (profile, "Database", "logid", " ") sqlca.LogPass = ProfileString (profile, "Database", "LogPassword", " ") CONNECT; if SQLca.SQLCode <> 0 then show_db_error("网络数据库连接错误") halt end if Messagebox("提示","数据库连接成功!") 新建一个窗体:命名为w_ini,改变title属性“ini文件的使用”。 添加一个datawindow控件命名为dw_browse;添加两个按钮,一个为“&R 取数”,名称为cb_retrieve,另一个为“&X 退出”,名称为cb_exit。 制作一个 datawindow,数据源是dbstandby服务器上的xhchis的一张表yp_base,用grid格式。制作出现在这样的datawindow。(打开d_yp_base)。 回到w_ini界面,将dw_browse控件的数据源设置为“d_yp_base”。 添加程序: 在cb_retrieve“取数”Button中添加程序: dw_browse.settransobject(sqlca) dw_browse.retrieve() 在cb_retrieve“取数”Button中添加程序: close(parent) 运行程序 |
||||