2011-4 by 邵家鑫 From Tsinghua 一种简单的powerbuilder10数据库编程介绍
1、需求分析与数据库建立
进行需求分析(需求分析文档),确定数据关系,建立各种数据表,建立数据库(Acce),设置ODBC数据源(控制面板->管理工具->数据源ODBC->“用户DSN”菜单下点“添加”选相应的数据源驱动程序,如果用Acce2003建立的,则选第三项“Driver do Microsoft Acce(*.mdb),然后点击“完成”->输入数据源名,如mydata,然后点击“选择”按钮选择建好的数据库,最后点“确定”,如下图所示)
2、界面设计(功能界面、重要数据表维护界面)
界面设计主要分为几个部分
(1)按照管理系统所需的功能设计界面。
首先画出所有可能的业务流程(数据的各种可能输入、修改、删除业务,数据的输出、显示业务)
(2)按照需要维护的表设计界面(往往给最高权限管理员直接修改数据用)
3、开始程序编写
(1) 新建一个workspace (new->workspace->workspace) (2) 在workspace下建立一个目标
(new->Target->application),可取名frame
2011-4 by 邵家鑫 From Tsinghua (3) 在目标下建一个主窗口
可取名w_main,窗体名一般以w_开头(new->PB Object->Window),将其Window Type设为“mdihelp!”
(4) 为主窗口建一个主菜单
可取名m_frame,菜单名一般以m_开头(new->PB Object->Menu) (5) 程序中设置ODB ODBC 点击按钮,选中“ODB ODBC”项,然后单击右边“New”按钮,弹出如下对话框,设置Profile Name和Data Source,如图所示。
(6) 建立配置文件
配置文件取名“config.ini”,内容如下:
[MyDB] DBMS=ODBC AutoCommit=False DBParm=ConnectString=\'DSN=mydata;UID=;PWD=\'
(7) 自动连接数据库与退出程序关闭数据库的编写
2011-4 by 邵家鑫 From Tsinghua 首先建立Global Variables如下: string gs_userid,gs_username //登录用户标识、用户姓名 string gs_root_path,gs_ini_path
//应用路径和主配置文件路径
其次申明Global External Functions如下:
FUNCTION int GetComputerNameA(ref string computername,ref long size) LIBRARY \"KERNEL32.DLL\" alias for \"GetComputerNameA;Ansi\" FUNCTION long GetCurrentDirectoryA( long nBufferLength, REF string szBuffer ) LIBRARY \"KERNEL32.DLL\" alias for \"GetCurrentDirectoryA;Ansi\" FUNCTION long SetCurrentDirectoryA( string szPathName ) LIBRARY \"KERNEL32.DLL\" alias for \"SetCurrentDirectoryA;Ansi\" //end prototypes 在程序的总入口(进入“”)Open事件中写入以下代码: // Profile moneyandfriends string ls_1
//设置应用根目录
gs_root_path = space(255) GetCurrentDirectoryA( 255, gs_root_path ) if right(gs_root_path,1) = \'\\\' then gs_root_path = left(gs_root_path,len(gs_root_path)1) if pos(ps_sql, \"group by \") > 0 then
ls_end = right(ps_sql, len(ps_sql)pos(ps_sql, \"order by \") + 1)
ls_where = mid(ps_sql, pos(ps_sql, \"where \")+6, len(ps_sql)-len(ls_front)-len(ls_end)-6) else
ls_where = mid(ps_sql, pos(ps_sql, \"where \")+6, len(ps_sql)-len(ls_front)-6) end if
2011-4 by 邵家鑫 From Tsinghua ls_return = ls_front + \"where (\" + ls_where +\") and \" + ps_where + \" \" + ls_end else if pos(ps_sql, \"group by \") > 0 then
ls_front = left(ps_sql, pos(ps_sql, \"group by \")pos(ps_sql, \"group by \") + 1) elseif pos(ps_sql, \"order by \") > 0 then
ls_front = left(ps_sql, pos(ps_sql, \"order by \")pos(ps_sql, \"order by \") + 1) else
ls_front = ps_sql
ls_end = \"\" end if ls_return = ls_front + \" where \" + ps_where + \" \" + ls_end end if return ls_return (2) 在窗口中声明局部变量用来记录最初的没有where语句的窗口的SQL查询语句 string is_original_sql
(3) 声明局部变量ls_old_select, ls_new_select,如下使用 string ls_old_select, ls_new_select ls_old_select = dw_ttxx.GetSQLSelect() if is_original_sql = \"\" then is_original_sql = ls_old_select else //避免条件被循环添加
ls_old_select = is_original_sql end if
ls_new_select = f_change_where(ls_old_select, is_conditions) dw_窗口.SetSQLSelect(ls_new_select)