powerbuilde将数据管道使应用程序具有在多个数据库中进行数据交换的功能。在powerbuilder6.0中,可以通过数据管道对象来执行指定的数据管道操作,这里就来设计一个简单的程序来实现说明在程序中数据管道的应用 。
sqlca是powerbuilder中缺少的事务处理对象,使用它可以进行数据库连接,其实它是一个由transaction创建的一个实例,但有时在应用程序中需要同时连接到不同的数据库,用户则必须使用两个或更多的事务处理对象,例如在此例中,执行pipe-examples数据管道操作必须连接到powersoft demo db v6和pipe两个数据库中,用户可以通过create语句创建两个不同的事务处理对象实例:
desttrans=create transaction // 连接目标数据库的事务处理对象
sourcetrans=create transaction // 连接源数据库的事务处理对象
在应用程序中必须通过数据管道对象来执行数据管道操作,同样用户可以使用create语句创建一个数据管道对象:
i-pipeline=create pipeline // 执行pipe-examples数据管道操作的数据管道对象 数据管道对象的几个常用的属性和函数现介绍如下:
dataobject属性:设置数据管道对象需要执行的数据管道操作,此例中我们将它设置为"pipe-examples"。
rowslnerror属性:该属性返回在执行数据管道操作时出现错误的记录总数。
rowsread属性:返回数据管道操作已经从源数据表中读取的记录总数。
rowswritten属性:返回数据管道操作已经写信目标数据库的记录总数。
start函数:开始数据管道操作,该函数的使用语汇法如下:
i-pipeline.start(sourcetrans,desttrans,dw-error)
sourcetrans参数代表连接源数据表的事务处理对象,desttrans参数是连接目标数据表的事务处理对象,dw-error是一个数据窗口控件,用来显示数据管道操作中出现错误的记录。
cancel函数:体制一个正进行的数据管道操作,该函数无参数,若操作正常停止,函数返回值为1,否则为1。
repair函数:该函数在数据管道操作出现错误后使用,repair函数将纠正操作中的错误并对数据库进行修复,使用语法如下:i-pipeline.repair(desttrans)
声明全局变量
程序中的事务处理对象和数据管道对象在各个事件中都要使用到,所以必须将它们设置为全局变量(global variables),在窗口的文本框中输入以下脚本定义 鍪挛翊矶韵笫道褪莨艿蓝韵笫道?br> transaction desttrans,sourcetrans
pipeline i-pipeline
创建程序窗口w_pipeline
在窗口中加入三个标签,一个数据窗口(dw_error)和四个按纽。其作用分另是三个标签用来显示数据管道执行过程中已读的记录总数、已写的记录总数和出现错误的记录数,“dw_error”数据窗口控件用来显示数据交换中的错误记录,[开始]、[终止]、和[取消]按钮分别用来控制数据管道操作的流程,[退出]按钮则是用来关闭应用程序.
编写程序代码
在pipe应用程序的open事件中添加以下代码:
open(w-pipeline) //打开程序窗口
程序中要执行连接数据库操作,所以必须设置两个事务处理对象的属性,具体方法是:切换到窗口绘图器,在程序窗口的open事件输入以下代码:
i-pipeline=create pipeline //创建数据管道对象实例
desttrans=create transaction //创建两个事务处理对象实例
sourcetrans=create transaction
//设置连接源数据库的事务处理对象实例的属性
sourcetrans.dbms =profilestring("regedit.ini","source","dbms"," ")
sourcetrans.database=profilestring("regedit.ini","source","database"," ")
sourcetrans.userid= profilestring ("regedit.ini", "source", "userid", "")
sourcetrans.dbpass= profilestring ("regedit.ini", "source", "dbpass", "")
sourcetrans.logid = profilestring ("regedit.ini", "source", "logid", "")
sourcetrans.logpass = profilestring ("regedit.ini", "source", "logpassword", "")
sourcetrans.servername = profilestring ("regedit.int", "source", "servername", "")
sourcetrans.dbparm =profilestring("regedit.ini","source","dbparm"," ")
connect using sourcetrans;//连接源数据库
if sourcetrans.sqlcode$#@60;0 then
messagebox(“错误!”,“无法连接源数据库!”)
end if
//设置连接目标数据库的事务处理对象实例的属性
desttrans.dbms =profilestring("regedit.ini","dest","dbms"," ")
desttrans.database=profilestring("regedit.ini","dest","database"," ")
desttrans.userid= profilestring ("regedit.ini", "dest", "userid", "")
desttrans.dbpass= profilestring ("regedit.ini", "dest", "dbpass", "")
desttrans.logid = profilestring ("regedit.ini", "dest", "logid", "")
desttrans.logpass = profilestring ("regedit.ini", "dest", "logpassword", "")
desttrans.servername = profilestring ("regedit.ini", "dest", "servername", "")
desttrans.dbparm =profilestring("regedit.ini","dest","dbparm"," ")
connect using desttrans; //连接目标数据库
if desttrans.sqlcode$#@60;0 then
messagebox(“错误!”,“无法连接目标数据库!”)
end if
在[开始]按钮进行数据管道操作,在clicked事件中写入如下代码:
i-pipeline.dataobject="pipe-examples" //设定需要执行的数据管道操作
i-pipeline.start(desttrans,desttrans,w-pipeline.dw-error) //执行操作
st-read.text=string(i-pipeline.rowsread) //显示数据管道操作的结果信息
st-write.text=string(i-pipeline.rowswritten)
st-error.text=string(i-pipeline.rowsinerror)
另外三个按钮单击(clicked)事件的代码分别如下:
//终止数据管道操作
integer i
i=i-pipeline.cancel()
if i$#@60;0 then
messagebox(“错误”,“终止数据管道操作错误!”)
end if
//纠正错误
i-pipeline.repair(desttrans)
//关闭程序窗口
close(w-pipeline)
//断开数据库连接
disconnect using desttrans;
if sourcetrans.sqlcode$#@60; $#@62;0 then
messagebox(“错误!”,“无法断开源数据库链接!”)
end if
//断开目标数据库连接
disconnect using desttrans;
if desttrans.sqlcode$#@60; $#@62;0 then
messagebox(“错误!”,“无法断开目标数据库链接!”)
end if
//删除三个对象实例
destroy sourcetrans
destroy desttrans
destroy i-pipeline
执行程序
程序代码编写完毕后执行程序,然后单击[开始]按钮,应用程序将开始数据管道操作,结果一段时间后位图窗口上部的三个标签中将显示出管道操作的结果。如何在应用程序中使用数据管道功能进行数据交换就讲到这里,如果你在使用powerbuilder开发的应用程序中需要进行数据库的复制或更新等等操作,那么建议你使用数据管道功能,因为它既可靠让你以最短的时间完成同样的功能。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器