|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionHave you ever tried to access an oracle instance in VBScript? This is not to complicated, but normally you need an additional file called tnsnames.ora in the oracle installation directory. This article describes an easy way to access an oracle instance without this file. All you have to do is use a different connection string. Using the codeYou can use this kind of connection string in VBScript, VBA and VisualBasic. I also tested it with Perl, it worked. Using a connection string without the need to have a correct tnsname.ora on your computer is espacially usefull for ad hoc scripting on many different database instances. Or when you cannot be shure if the user has a correct tnsnames.ora on his computer. First take a look at the standard connection string for oracle, used in a VBScript file: Dim strCon
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=MYDB; uid=read;pwd=read;"
You see in the first section the driver name (Microsoft ODBC for Oracle),
followed by the alias for the instance and last the oracle username and
password. The alias name MYDB.WORLD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = mysrv)(PORT = 7001))
)
(CONNECT_DATA =
(SERVICE_NAME = MYDB)
)
)
Oracle uses this definition to determine which physical database instance is
associated with the given alias Dim strCon
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=mysrv)(PORT=7001))" & _
"(CONNECT_DATA=(SERVICE_NAME=MYDB))); uid=read;pwd=read;"
Now you will be able to access every oracle instance, without the need to have a correct tnsnames.ora on your client machine. Finally here is the complete source code that selects some rows and writes them to STDOUT: Dim strCon
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=mysrv)(PORT=7001))" & _
"(CONNECT_DATA=(SERVICE_NAME=MYDB))); uid=read;pwd=read;"
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Set oRs = oCon.Execute("SELECT myfield FROM mytable)
While Not oRs.EOF
WSCript.Echo oRs.Fields(0).Value
oRs.MoveNext
Wend
oCon.Close
Set oRs = Nothing
Set oCon = Nothing
I hope that you found this article useful. For me this kind of connection
string was already usefull, when I could not be shure if the end-user had a
correct tnsnames.ora on his computer.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||