Connecting to Oracle without Configuring the tnsnames.ora File

Connecting to Oracle without Configuring the tnsnames.ora File

Have you ever wanted to connect to a database without dealing with Oracle’s annoying reliance on the tnsnames.ora file?

The trick is to put the tnsnames information into the ODBC connection string directly, using the Data Source=… option.

The following (untested) code is VB, but the concept should work in any language that supports ODBC:

    Dim TNS_INFO As String    Dim cnxDB As New ADODB.Connection        TNS_INFO = "(DESCRIPTION=" & _                       "(ADDRESS_LIST=" & _                       "(ADDRESS=(PROTOCOL=TCP)" & _                       "(" & _                       "(PORT=1521)))" & _                       "(CONNECT_DATA=(SID=prod_tst)" & _                       "(SERVER=DEDICATED)))"        cnxDB.ConnectionString = "Provider=OraOLEDB.Oracle;" & _                       "Data Source=" & TNS_INFO & ";" & _                       "user id=cressman;" & _                       "password=mean2me"    Debug.Print cnxDB.ConnectionString    cnxDB.Open
See also  5 Benefits of Using Time Tracking Software

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist