Question

I'm trying to test if the connection from my utility to oracle DB is working by using .dsn . I've searched from many forums but to no avail . Here is my code:

    Dim filedsn As String = "C:\my_dsn.dsn"
    Dim uid As String = "id123"
    Dim pwd As String = "1234"

    Dim cn As OdbcConnection
    cn = New OdbcConnection("Driver=Oracle in OraClient11g_home2;Provider=msdaora;dsn=" & filedsn & ";uid=" & uid & ";pwd=" & pwd & ";")
    Dim mystring As String = "Select * from DD_CADS1.PDTABLE_12_1001"
    Dim cmd As OdbcCommand = New OdbcCommand(mystring)
    cn.Open()
    MsgBox("Connected")
    cn.Close()

But these error appears..

ERROR [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error

ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error

Can you tell me what's the problem with my codes? Is there any other way to connect to oracle database through .dsn using VB.net? I'm currently using ** Oracle 11g**.

Was it helpful?

Solution

The biggest problem in your code is that you use ODBC to connect to Oracle. You should use ODP.NET, which is even recommended by Microsoft. What you doing is there [most likely] for backwards compatibility and not to write the new stuff.

Here your problem is in connection string, which should look like this

 "DSN=oracledsn;UID=myUID;PWD=myPWD;Integrated Security=no;"

And you don't need command just to test connection

using cn= New OdbcConnection(".....")

    cn.Open() 
    if cn.State = ConnState.Open Then MessageBox.Show(...)
end using

When you use using it will take care of closing and disposing of connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top