【编程开发】AspAsp.NetCGIPHPJspXMLPERLC++C#VCVBDelphiPowerBuilderJAVA汇编数据库编程移动开发其它语言

您现在的位置:首页 > 网络学院 > 编程开发 > 数据库编程 > 在PB中利用DataWindow

在PB中利用DataWindow

来源: 作者: 日期:2006-08-14

【聚杰网数据库编程】在PB中利用DataWindow


P>随着MIS开发工具的日益完善,人们对信息检索的要求也越来越高,以往每次查询时都由用户输入,然后进行检索的方式已越来越无法满足用户的要求,许多用户都希望系统具有自动记忆查询条件的功能,这样经常使用的查询条件就不必重复输入了。
  PowerBuilder(以下简称PB)中的DataStore(数据存储)可以帮助开发人员方便地解决这一问题。PB作为C/S环境下的数据库开发工具,其面向对象、可视化以及方便移植等特点引人注目,成为目前非常流行的数据库前端开发工具。本文以一个文献索引数据库为实例,详细介绍如何在PB中利用DataStore实现查询条件的记忆。

  DataStore是非可视的数据窗口控件,它能够为应用程序存储数据而不消耗任何图形资源。除了一些可视特征外,它的功能及行为与数据窗口控件相同。因此,我们可以利用DataStore的特点方便地记忆和维护查询条件。

  第一步,在数据库中建立一个查询条件表,其中包含每个要用到的条件字段(如标题、文献来源、开始日期、结束日期等)和一个日期字段、时间字段。该表用来保存最近使用的10个(如果需要可以保存任意个)不同条件,每个条件就是一条记录,后两个字段(日期、时间)用来记载条件的使用时间。

  第二步,创建一个包含查询条件表中所有字段的数据窗口对象 dw_condation,按使用时间的先后排序。进行查询条件设置时,

  第三步,构造如下图的查询条件设置窗口。

  四个复选框对应四个条件字段,左、右方向按钮用来浏览设置最近使用过的条件。

  第四步,为窗口事件及各控件编写相应的程序。

  定义窗口实例变量:

datastore ids_Datalong currentrow窗口的Open事件:ids_Data = Create DataStore//创建数据存储实例ids_Data.DataObject = "dw_condation"//将dw_condation数据窗口对象与ids_Data连接ids_Data.SetTransObject(sqlca)ids_data.retrieve()if ids_data.rowcount() $#@62;1 then //设置当前条件为最后一次使用的条件pb_3.enabled=truecurrentrow=ids_data.rowcount()trigger event ue_setquery(ids_data.rowcount())end if窗口自定义事件ue_setquery:string bt,source//标题、文献来源date start_day,end_day//开始日期、结束日期long page//页号//根据参数rownum从数据存储实例中得到各条件值bt=ids_Data.Object.bt[rownum]source=ids_Data.Object.source[rownum]start_day=ids_Data.Objec   egin[rownum]end_day=ids_Data.Object.end[rownum]page=ids_Data.Object.page[rownum]//初始化各窗口控件的状态cbx_1.checked=falsecbx_2.checked=falsecbx_3.checked=falsecbx_4.checked=falsesle_1.enabled=falseddlb_1.enabled=falseem_1.enabled=falseem_2.enabled=falseem_3.enabled=false//分别设置各个条件值if not isnull(bt) and trim(bt)$#@60; $#@62;"" thencbx_1.checked=truesle_1.enabled=truesle_1.text=btend ifif not isnull(source) and trim(source)$#@60; $#@62;"" thencbx_2.checked=trueddlb_1.enabled=trueddlb_1.text=sourceend ifif isnull(start_day) or start_day=date("1900-1-1") then em_1.text=string(today(),"yyyy-mm-dd")elsecbx_3.checked=trueem_1.enabled=trueem_1.text=string(start_day,"yyyy-mm-dd")end ifif isnull(end_day) or end_day=date("1900-1-1") thenem_2.text="2050-1-1"elsecbx_3.checked=trueem_2.enabled=trueem_2.text=string(end_day,"yyyy-mm-dd")end ifif not isnull(page) and page$#@60; $#@62;0 thencbx_4.checked=trueem_3.enabled=trueem_3.text=string(page)end if窗口的Close事件:destroy ids_data//清除数据存储实例pb_3(向前按钮)的Clicked事件:if currentrow $#@62;1 then //设置前一个条件为当前条件currentrow=currentrow - 1trigger event ue_setquery(currentrow)pb_4.enabled=trueif currentrow=1 then this.enabled=falseend ifpb_4(向后按钮)的Clicked事件:if currentrow$#@60; ids_data.rowcount() then //设置后一条件为当前条件currentrow=currentrow + 1pb_3.enabled=trueif currentrow=ids_data.rowcount()then this.enabled=falsetrigger event ue_setquery(currentrow)end ifpb_1(确定按钮)的Clicked事件:string bt,source,sqltext,sql_all,sql_title,sql_source,sql_date,sql_pagedate start_day,end_daylong page,rownum,rowcountboolean exist//根据用户设置的条件动态生成查询SQL语法if cbx_1.checked thenbt=sle_1.textsql_title="index"."title" like +"%"+bt +"%"end ifif cbx_2.checked then source=ddlb_1.textsql_source= and "index"."source" like +"%"+source +"%"end ifif cbx_3.checked then start_day=date(em_1.text)end_day=date(em_2.text)sql_date=and"index"."date" $#@62;=+""+string(start_day,"yyyy.mm.dd") +""+and +"index"."date"$#@60; =+""+string(end_day,"yyyy.mm.dd") +"" end ifif cbx_4.checked then page=long(em_3.text)sql_page= and "index"."page"=+string(page)end ifsqltext=SELECT "index"."num",  &+"index"."title",  & +"index"."source",  & +"index"."date",  & +"index"."page",  & +"index"."other"  & + FROM "index"  & + WHERE +sql_title +sql_source +sql_date+sql_page & + ORDER BY "index"."num" ASC rowcount=ids_data.rowcount()for rownum=1 to rowcount//判断该条件是否已经存在if ids_data.object.data[rownum,1]=bt and &ids_data.object.data[rownum,2]=source and &ids_data.object.data[rownum,3]=start_day and &ids_data.object.data[rownum,4]=end_day and &ids_data.object.data[rownum,5]=page thenids_data.object.data[rownum,6]=now()ids_data.object.data[rownum,7]=today()exist=trueexitend ifnextif not exist then//条件不存在,记忆新条件if rowcount$#@60; 10 thenelseids_data.deleterow(1)end ifrownum=ids_data.insertrow(0)ids_data.object.data[rownum,1]=btids_data.object.data[rownum,2]=sourceids_data.object.data[rownum,3]=start_dayids_data.object.data[rownum,4]=end_dayids_data.object.data[rownum,5]=pageids_data.object.data[rownum,6]=now()ids_data.object.data[rownum,7]=today()end ifids_data.update()//根据生成的SQL语句,修改主窗口中的数据窗口语法,进行查询w_index.dw_1.setsqlselect(sqltext)close(parent)if isvalid(w_index) then w_index.dw_1.retrieve()pb_2(取消按钮)的Clicked事件:close(parent)
  以上程序在Pb6.5、Sybase SQL Anywhere5.0下运行通过,读者对它稍加修改就可应用在自己开发的MIS系统中。


评论   点击查看全部评论
您的评论参与,将为聚杰带来更大的动力!请不要吝啬!
快速回复
请使用文明语言让我们维护健康绿色网络环境!

匿名发表   验证码: