1、powerbuilder编程简单入门个人总结一种简单的powerbuilder10数据库编程介绍1、需求分析与数据库建立进行需求分析(需求分析文档),确定数据关系,建立各种数据表,建立数据库(Access),设置ODBC数据源(控制面板-管理工具-数据源ODBC-“用户DSN”菜单下点“添加”选相应的数据源驱动程序,如果用Access2003建立的,则选第三项“Driver do Microsoft Access(*.mdb),然后点击“完成”-输入数据源名,如mydata,然后点击“选择”按钮选择建好的数据库,最后点“确定”,如下图所示)2、界面设计(功能界面、重要数据表维护界面) 界面设计
2、主要分为几个部分(1)按照管理系统所需的功能设计界面。 首先画出所有可能的业务流程(数据的各种可能输入、修改、删除业务,数据的输出、显示业务)(2)按照需要维护的表设计界面(往往给最高权限管理员直接修改数据用)3、开始程序编写(1) 新建一个workspace(new-workspace-workspace)(2) 在workspace下建立一个目标(new-Target-application),可取名frame(3) 在目标下建一个主窗口可取名w_main,窗体名一般以w_开头(new-PB Object-Window),将其Window Type设为“mdihelp!”(4) 为主窗口建
3、一个主菜单可取名m_frame,菜单名一般以m_开头(new-PB Object-Menu)(5) 程序中设置ODB ODBC点击按钮,选中“ODB ODBC”项,然后单击右边“New”按钮,弹出如下对话框,设置Profile Name和Data Source,如图所示。(6) 建立配置文件配置文件取名“config.ini”,内容如下:MyDBDBMS=ODBCAutoCommit=FalseDBParm=ConnectString=DSN=mydata;UID=;PWD=(7) 自动连接数据库与退出程序关闭数据库的编写 首先建立Global Variables如下:string gs_us
4、erid,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;AnsiFUNCTION long GetCurrentDirectoryA( long nBufferLength, REF string szBuf
5、fer ) LIBRARY KERNEL32.DLL alias for GetCurrentDirectoryA;AnsiFUNCTION long SetCurrentDirectoryA( string szPathName ) LIBRARY KERNEL32.DLL alias for SetCurrentDirectoryA;Ansi/end prototypes在程序的总入口(进入“”)Open事件中写入以下代码:/ Profile moneyandfriendsstring ls_1/设置应用根目录gs_root_path = space(255)GetCurrentDirec
6、toryA( 255, gs_root_path )if right(gs_root_path,1) = then gs_root_path = left(gs_root_path,len(gs_root_path) - 1)end if/设置配置文件路径gs_ini_path = gs_root_path + config.iniIF not FileExists ( gs_ini_path ) THEN MessageBox( 找不到配置文件 +gs_ini_path,系统配置错误,stopsign!,ok! )RETURNEND IF/SetProfileString(gs_ini_pa
7、th,DBMS,ls_1=ProfileString(gs_ini_path,MyDB,DBMS,ODBC)SQLCA.DBMS =ls_1SQLCA.AutoCommit = Falsels_1=ProfileString(gs_ini_path,MyDB,DBParm,error)SQLCA.DBParm = ls_1connect using sqlca;open(w_main) /打开主界面在Close事件中加入如下代码:disCONNECT USING sqlca;(8) 建立新的具体功能窗体(如w_zichuangti)8.1 设置窗体BackColor为“Cream”,输入窗体T
8、itle;要在1024*768分辨率下基本满屏,窗体大小可设置为“4645*2748” 8.2在新窗体上一般用GroupBox来划分功能区域,设置该控件的字体为“宋体”,大小为“10”,背景颜色为“Cream” 8.3用静态文本做功能说明或指示,一般加黑,宋体9号字 示例程序界面如下:(9) 响应菜单click事件弹出子窗口(相同子窗口只弹出一次待研究)在菜单下编写代码打开窗体(双击菜单即可在Click事件下编写),简单代码如下:/选择菜单时调出子窗体window lwopensheet(lw,w_ zichuangti,parentwindow,0,Cascaded! )lw.WindowS
9、tate = Maximized! (10) 相同的子窗口只让弹出一次(代研究)(10) 建立数据窗口(11) 添加新的pbl文件 一般要用不同的pbl文件分类存储不同窗体、数据窗口和菜单等资源;建立新的pbl文件的方法如下:点击工具栏上“Library”按钮,然后在弹出子窗口中进入程序所在文件夹,在工具栏左下有“”创建pbl的按钮。创建好pbl库文件后在目标(如果按前面建立名称为frame的目标,则在“”)上单击右键,选择属性在弹出窗口中可添加Library List,如下图,也可在这里建立新的pbl库文件(12) 如何编译出可运行程序 首先新建一个Project(File-new-Proj
10、ect-Application),如取名p_main 然后打开新建的Project,设置可执行文件生成目录。(13) DataWindow窗口连通数据库 dw_1.settransobject(sqlca)(14) 格式化日期(2007-10-05变为20071005)f_format_date函数,输入string变量ps_date,返回stringif isnull(ps_date) then return if len(ps_date) 0 then if MessageBox(提示,数据没有保存,是否不保存退出?,Question!,YesNo!) =2 then return 1 e
11、nd ifend if4、其他编程4.1 将数据库中数据显示到dropdownlistbox的下拉菜单里建立“f_ddlb_populate”函数,函数输入两个变量:dropdownlistbox:ddlb_this 要显示到的空间名string:as_sql 查询数据库的sql语句函数代码如下:ddlb_this.reset()ddlb_this.additem()string ls_disp, ls_dataDECLARE my_cursor DYNAMIC CURSOR FOR SQLSA ;PREPARE SQLSA FROM :as_sql ;OPEN DYNAMIC my_curs
12、or ;FETCH my_cursor INTO :ls_disp, :ls_data;if isnull(ls_disp) then ls_disp = if isnull(ls_data) then ls_data = do while sqlca.sqlcode = 0 ddlb_this.additem(trim(left(ls_disp+space(200),200) + ls_data) FETCH my_cursor INTO :ls_disp, :ls_data;loopCLOSE my_cursor ; as_sql语句里面需要有两个输出 如果ddlb的数据项比较多,一般选中
13、VScrollBar如下调用这个函数:string ls_sqlls_sql=select zcr_bh, from zcr order by zcr_bh; /举例f_ddlb_populate(ddlb_bh,ls_sql) /将ddlb_bh列表框里写入列表项4.2 数据窗口常用几个函数 dw_deal.settransobject(sqlca) /连接数据库 dw_deal.insertrow(number row):在指定行之前插入一行,如果要在最后一行新增一行,取row=0 dw_deal.deleterow(number row):删除指定行,row=0时删除当前行dw_deal
14、.rowcount():返回数据窗口中总行数 dw_deal.setitem(li_n,lxr_zpdz,pic) /设置某行某列的数据 integer dwcontrol.AcceptText ( ):Applies the contents of the DataWindow controls edit control to the current item in the DataWindow buffer. integer dwcontrol.Filter ( ):根据给定过滤条件来列出满足条件的记录,过滤条件由SetFilter函数指定integer dwcontrol.SetFilt
15、er ( string format ):设定过滤条件integer dwcontrol.GetColumn ( ):Returns the number of the current column in the DataWindow controlstring dwcontrol.GetColumnName ( ):Returns the name of the current column in the DataWindow control.date dwcontrol.GetItemDate ( long row, string column , DWBuffer dwbuffer ,
16、boolean originalvalue )date dwcontrol.GetItemDate ( long row, integer column , DWBuffer dwbuffer, boolean originalvalue )GetItemStringGetItemString():long dwcontrol.GetNextModified (long row, DWBuffer dwbuffer ):SetRow(long row): 设定指定行为当前行GetRow() :获得数据窗口中当前行GetSelectedRow ( long row ):获得指定行row后面第一个
17、被选中的行,如没有则返回零integer dwcontrol.SelectRow ( long row, boolean select ):选中指定行IsSelected ( long row ):判断某行是否被选中ScrollToRow ( number row ):窗口数据滚动在指定行,如果row=0则规东到第一行 integer dwcontrol.RowsMove ( long startrow, long endrow, DWBuffer , datawindow targetdw, long beforerow, DWBuffer targetbuffer ) 将一个数据窗口的几行
18、数据移到另一个数据窗口。movebuffer和DWBuffer的范围为Primary!、Delete!和Filter!string dwcontrol.Describe ( string propertylist ):返回数据窗口中控件属性string dwcontrol.Modify ( string modstring ) :修改数据窗口中控件属性integer dwcontrol.SetItemStatus(long row, integer column, dwbuffer dwbuffer, dwitemstatus status):改变数据状态,dwitemstatus有以下四种状
19、态NotModified! ,DataModified! ,New!,NewModified! 4.3 数据窗口常用几个事件 RetrieveRow event (DataWindows):在一行被检索后响应;返回0则继续,返回1则停止检索4.4 常用SQL语句SELECT *FROM financesWHERE description LIKE gs_ ESCAPE S这里ESCAPE S表示不将s后面的通配符_作为通配符,而只是作为一般的字符4.5 给数据窗口增加条件The following scripts dynamically add a WHERE clause to a Data
20、Window object that was created with a SELECT statement that did not include a WHERE clause. (Since this example appends a WHERE clause to the original SELECT statement, additional code would be needed to remove a where clause from the original SELECT statement if it had one.) This technique is usefu
21、l when the arguments in the WHERE clause might change at execution time. The original SELECT statement might be:SELECT employee.emp_id, employee.l_name FROM employeePresumably, the application builds a WHERE clause based on the users choices. The WHERE clause might be:WHERE emp_id 40000The script fo
22、r the windows Open event stores the original SELECT statement in original_select, an instance variable:dw_emp.SetTransObject(SQLCA)original_select = & dw_emp.Describe(DataWindow.Table.Select)The script for a CommandButtons Clicked event attaches a WHERE clause stored in the instance variable where_c
23、lause to original_select and assigns it to the DataWindows Table.Select property:string rc, mod_stringmod_string = DataWindow.Table.Select= & + original_select + where_clause + rc = dw_emp.Modify(mod_string)IF rc = THEN dw_emp.Retrieve( )ELSE MessageBox(Status, Modify Failed + rc)END IF4.6 常用PowerSc
24、ript语句1) CHOOSE CASE语句CHOOSE CASE Weight CASE IS 0 then ls_front = left(ps_sql, pos(ps_sql, where ) - 1) if pos(ps_sql, group by ) 0 then ls_end = right(ps_sql, len(ps_sql) - pos(ps_sql, group by ) + 1) ls_where = mid(ps_sql, pos(ps_sql, where )+6, len(ps_sql)-len(ls_front)-len(ls_end)-6) elseif pos
25、(ps_sql, order 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 ls_return = ls_front + where ( + l
26、s_where +) and + ps_where + + ls_endelse if pos(ps_sql, group by ) 0 then ls_front = left(ps_sql, pos(ps_sql, group by ) - 1) ls_end = right(ps_sql, len(ps_sql) - pos(ps_sql, group by ) + 1) elseif pos(ps_sql, order by ) 0 then ls_front = left(ps_sql, pos(ps_sql, order by ) - 1) ls_end = right(ps_sq
27、l, len(ps_sql) - pos(ps_sql, order by ) + 1) else ls_front = ps_sql ls_end = end if ls_return = ls_front + where + ps_where + + ls_endend ifreturn ls_return(2)在窗口中声明局部变量用来记录最初的没有where语句的窗口的SQL查询语句string is_original_sql(3) 声明局部变量ls_old_select, ls_new_select,如下使用string ls_old_select, ls_new_selectls_old_select = dw_ttxx.GetSQLSelect()if is_original_sql = then is_original_sql = ls_old_selectelse /避免条件被循环添加 ls_old_select = is_original_sqlend if ls_new_select = f_change_where(ls_old_select, is_conditions)dw_窗口.SetSQLSelect(ls_new_select)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1