连接Oracle还要设置tnsname.org很不方便,特别是多用户发布时尤其的不方便。我们可以写个程序直接将配置写到tnsname.org中或没次启动程序时动态写入。思路基本是这样,以下只是一个function,大家可以灵活的应用。例如在程序启动时利用FileCopy备份tnsname.org,然后用Replace!的方式重写配置,Connect之后再恢复。
/* -----------------------------------------------------------------------------
作者:wwwfind 2005-04-04
函数: f_connect
参数: s_name 配置名
s_server 服务器名或IP
s_database 全局数据库名称
返回值: 连接成功返回Ture,失败返回False
说明: 通过动态的改写tnsnames.ora文件免除安装时手动的配置.
-------------------------------------------------------------------------------*/
integer li_FileNum
string s_Path,ls_ConnectString,s_cont
//获取文件路径
RegistryGet("HKEY_LOCAL_MACHINE/SOFTWARE/ORACLE","ORACLE_HOME",RegString!,s_path)
s_path=trim(s_path)+"/network/admin/tnsnames.ora"
li_FileNum = FileOpen(trim(s_path),StreamMode!,Read!,LockReadWrite!)
if li_FileNum>0 then
//生成配置字符串
//ls_ConnectString="# TNSNAMES.ORA Network Configuration File:Application Setup"+char(13)+char(10)
//ls_ConnectString+="# Generated by Oracle configuration tools."+char(13)+char(10)+char(13)+char(10)
ls_ConnectString+=s_name+" ="+char(13)+char(10)
ls_ConnectString+=" (DESCRIPTION ="+char(13)+char(10)
ls_ConnectString+=" (ADDRESS_LIST ="+char(13)+char(10)
ls_ConnectString+=" (ADDRESS = (PROTOCOL = TCP)(HOST = "+s_server+")(PORT = 1521))"+char(13)+char(10)
ls_ConnectString+=" )"+char(13)+char(10)
ls_ConnectString+=" (CONNECT_DATA ="+char(13)+char(10)
ls_ConnectString+=" (SERVICE_NAME = "+s_database+")"+char(13)+char(10)
ls_ConnectString+=" )"+char(13)+char(10)
ls_ConnectString+=" )"+char(13)+char(10)
FileRead(li_FileNum,s_cont)
FileClose(li_FileNUm)
if Pos(Upper(s_cont),Upper(s_name+" ="+char(13)+char(10)))<=0 then
li_FileNum = FileOpen(trim(s_path),StreamMode!,Write!,LockReadWrite!,Append! )
FileWrite(li_FileNum,ls_ConnectString )
FileClose(li_FileNum)
end if
return true
else
Post MessageBox("提示","打开文件"+s_path+"失败,请检查。")
return false
end if