选择显示字体大小

pb6中怎样实现用代码配置odbc

一、 引言

powerbuilder是客户端的开发工具,在实际应用中必须与数据库管理系统配合起来才能运行应用程序,它与数据库的连接可以通过odbc接口实现。在odbc中配置好数据源后,便可在程序代码中连接数据库并对数据库进行各种操作了。

怎样才能在代码中实现对odbc的配置呢?其实,windows中所有的odbc数据源在系统注册表中都有记载,因此只要对注册表中的项目进行相应的修改即可。与odbc相关的键中,hkey_local_machine\software\odbc\odbcinst.ini\odbcdriver中记录了所有已安装的odbc驱动程序;hkey_local_machine\software\odbc\odbcinst.ini中记录了各种odbc驱动程序的相关信息;hkey_current_user\software\odbc\odbc.ini\odbc data sources记录了各种数据源的类型;hkey_current_user\software\odbc\odbc.ini则记录了各种数据源的详细信息。因此,只要在代码中对这些键进行配置,便可达到手工配置odbc的目的。

二、在代码中配置odbc的实现

下面以powerbuilder内置的sybase sql anywhere5.0数据库为例介绍odbc的代码配置方法。

1、 找出数据库驱动程序及相关文件

首先找到相应的数据库驱动程序,这些文件可以在注册表的hkey_local_machine\software\odbc\odbcinst.ini下相应键中找到。如sybase sql anywhere5.0为dbeng50.exe,wod50t.dll,以及一些其他相关的动态链接库,此例中为dbl50t.dll,wl50ent.dll,wtr50t.dll,wodbc.gid,wtr50t.dll。这些文件必须与最终的可执行程序一起安装到用户的计算机上,可以与应用程序放在一起,也可以放在一个特定的目录下(本例为windows的system目录下)。在调试时可先将这些文件复制到指定的目录下。

2、 在程序中用代码配置odbc

在应用的open事件中对odbc配置的代码如下:

ulong ul_num

int answer,answer1,answer2,answer3,answer4,answer5,ansapp

string ls_driver,ls_start,ls_location,apppath,db_path,odbcstr

//获取操作系统的system目录,并保存到ls_location变量

answer=registryget(“kyey_local_machine\software\microsoft\windows\currentversion\setup”,”sysdir”,regstring!ls_location)

if answer=-1 then

messagebox(‘错误’,’应用程序无法获取windows的系统目录,系统统将终止运行!’,stopsign!)

return

end if

//判断sybase sql anywhere5.0的驱动程序是否正确安装

ls_driver=ls_location+’\wod50t.dll’

ls_start=ls_location+’\dbeng50.exe’

if not (fileexists(ls_driver) and fileexists(ls_start)) then

messagebox(‘错误’,’系统中没有安装sql anywhere的驱动程序,系统将终止运行!’,stopsign!)

return

end if

//设置odbc\odbcinst.ini\odbc drivers

answer=registryset(‘ hkey_local_machine\software\odbc\odbcinst.ini\odbcdriver’,’sybase sql anywhere 5.0’,regstring!,’installed’)

if answer=-1 then

messagebox(‘错误’,’应用程序无法设置odbc drivers,系统将终止运行!’,stopsign!)

return

end if

//设置odbc\odbcinst.ini\

answer1=registryset(‘ hkey_local_machine\software\odbc\odbcinst.ini\sybase sql anywhere 5.0’,’cptimeout’,regstring!,’not pooled’)

answer2=registryset(‘ hkey_local_machine\software\odbc\odbcinst.ini\sybase sql anywhere 5.0’,’driver’,regstring!,ls_driver)

answer3=registryset(‘ hkey_local_machine\software\odbc\odbcinst.ini\sybase sql anywhere 5.0’,’setup’,regstring!,ls_driver)

if answer1=-1 or answer2=-1 or answer3=-1 then

messagebox(‘错误’,’应用程序无法设置odbcinst.ini,系统将终止运行!’,stopsign!)

return

end if

//设置odbc data source名称(anysql为数据源名)

answer=registryset(‘ hkey_local_machine\software\odbc\odbcinst.ini\odbc data sources’,’anysql’,regstring!,’sybase sql anywhere 5.0’)

if answer=-1 then

messagebox(‘错误’,’应用程序无法设置odbc data source名称,系统将终止运行!’,stopsign!)

return

end if

//获取应用程序的安装路径(ybinput为应用程序安装到用户计算机上后在注册表中注册的应用程序名,yibiao.db为应用程序的数据库名,与应用程序在同一目录下,anysql为数据源名)

ansapp=registryget(“kyey_local_machine\software\microsoft\windows\currentversion\app paths\ybinput”,”path”,regstring!app_path)

if ansapp=-1 then

messagebox(‘错误’,’应用程序无法获取安装路径名,系统将终止运行!’,stopsign!)

return

else

db_path=app_path+’yibiao.db’

end if

//设置odbc.ini的细节

answer1=registryset(‘hkey_current_user\software\odbc\odbc.ini\anysql’,’driver’,regstring!,ls_driver)

answer2=registryset(‘hkey_current_user\software\odbc\odbc.ini\anysql’,’start’,regstring!,ls_start)

answer3=registryset(‘hkey_current_user\software\odbc\odbc.ini\anysql’,’autostop’,regstring!,’yes’)

answer4=registryset(‘hkey_current_user\software\odbc\odbc.ini\anysql’,’databasefile’,regstring!,db_path)

answer5=registryset(‘hkey_current_user\software\odbc\odbc.ini\anysql’,’databasename’,regstring!,’yibiao’)

if answer1=-1 or answer2=-1 or answer3=-1 or answer4=-1 or answer5=-1 then

messagebox(‘错误’,’应用程序无法设置odbc.ini细节,系统将终止运行!’,stopsign!)

return

end if

odbcstr=’dsn=anysql;databasename=yibiao;databasefile=’+db_path

sqlca.dbms=’odbc’

sqlca.databse=’yibiao’

sqlca.dbparm=”connectstring=’”+odbcstr+”;uid=dba;pwd=sql’”

connect using sqlca;

open(w_main)

该程序在powerbuilder6.0下运行通过。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons