今天觉得有必要写一下日志,因为写了第一个PB程序,通过ini文件连接到ORACLE,虽然还只是一个检索数据的功能,不过也不错了,断断续续的学PB也学了有一段日子了,终于有一个成品了,功能简简单单的挺不错的。

觉得还是有些步骤的:

1、先建立一个工程:Workspace - Target -

其他文件。

2、直接从PB导出ini的数据库连接文件,看网上有些人说不可以导出,不过可能是版本不同或者是其他的原因吧,反正我是直接的用PB连接数据库然后导出一个ini的文件,当然在导出后手工删除了一些无用的信息:

直接导出数据库连接文件的方法:

1)在PB中连接到目的数据库,然后点击右键,选择“Export

Profile(s)”,在弹出的“Export Profile(s)”窗口中已经默认选中了当前连接的数据库,直接点击“OK”

2)在弹出的“Select Profile

File”窗口中选择ini文件的路径,一般就是程序的目录下,填写ini文件的名字,譬如dblink,就会生成一个dblink.ini的文件

3)打开dblink.ini文件,内容如下

[DBMS_PROFILES]

Profiles=mzsf

[Profile mzsf]

DBMS=O90 Oracle9i (9.0.1)

Database=

UserId=

DatabasePassword=

LogId=mzsf

LogPassword=test

ServerName=testserver

DBParm=PBCatalogOwner='test'

Lock=

Prompt=FALSE

AutoCommit=FALSE

可按具体情况进行修改,譬如删除

[DBMS_PROFILES]

Profiles=mzsf

[Profile mzsf]

然后再添加上[Database],完整内容如下

[Database]

DBMS=O90 Oracle9i (9.0.1)

Database=

UserId=

DatabasePassword=

LogId=mzsf

LogPassword=test

ServerName=testserver

DBParm=PBCatalogOwner='test'

Lock=

Prompt=FALSE

AutoCommit=FALSE

3、写入读取ini文件的语句,使用默认的SQLCA。

可以在需要的地方写上如下内容(譬如在Application的open事件中)

string str_file

//获取数据库配置文件,目录相对当前应用程序根目录

str_file = "dbtest.ini"

// 判断指定的INI文件是否存在

if not FileExists (str_file) then

messagebox("提示!","ini文件不存在!",exclamation!,ok!,2)

return

end if

SQLCA.DBMS = ProfileString(str_file,"database","DBMS"," ")

SQLCA.Database =

ProfileString(str_file,"database","DataBase"," ")

SQLCA.LogID = ProfileString(str_file,"database","LogID"," ")

SQLCA.LogPass = ProfileString(str_file,"database","LogPassword"," ")

SQLCA.ServerName = ProfileString(str_file,"database","ServerName","

")

SQLCA.UserID = ProfileString(str_file,"database","UserID"," ")

SQLCA.DBPass = ProfileString(str_file,"database","DatabasePassword", " ")

SQLCA.Lock = ProfileString(str_file,"database","Lock"," ")

SQLCA.DbParm = ProfileString(str_file,"database","dbparm"," ")

SQLCA.autocommit = false

//默认使用SQLCA,否则应该

//connect using ...

connect;

open(w_login)

在必要的地方关闭连接,譬如Application的close事件

//默认使用SQLCA,否则应该

//disconnect using ...

disconnect;

4、添加窗口、控件,使用SQL语句读取数据。

为了在下面一步中点击exe文件也能连接到数据库,在放置数据窗口对象的窗口中选中数据窗口控件,在open事件中添加如下代码

dw_1.settrans(sqlca)

dw_1.retrieve( )

5、生成exe和dll文件。

选择“File - New”,在“New”窗口中选择“Project”标签页,选择“Application

Wizard”,其余步骤按提示完成即可,最后关闭所有的窗口,在当前项目工作区上点击右键,选择“Deploy”,就能生成exe和dll文件了(在“Application

Wizard”中设置了生成exe和dll)。

6、基本就完成一个简单的连接数据库的系统。

更多推荐