Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VB.Net - Connection string to Oracle 11g dbase

Hi

In my previous VB6 application, i was able to connect to my oracle 11g tables using the connection:
oconn.Open "Provider=OraOLEDB.Oracle.1;Data Source=DataSouceName;User Id=UserId;Password=Password;"

Open in new window


Now that i'm creating a new application using Visual Studio 2019 with a vb form and a DataGridView1, i would like to pull data the same way.

So i tried with:
Imports System.Data.SqlClient
Imports OracleConnection = Oracle.ManagedDataAccess.Client.OracleConnection
Imports OracleCommand = Oracle.ManagedDataAccess.Client.OracleCommand
Imports OracleDataReader = Oracle.ManagedDataAccess.Client.OracleDataReader

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        cn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Data Source=DataSourceName;User Id=UserId;Password=Password;"
        Try
            cn.Open()

            Dim da As New SqlDataAdapter("Select * from Location", cn)
            Dim dt As New DataTable


            da.Fill(dt)
              DataGridView1.DataSource = dt


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window

But i'm getting the error:
System.ArgumentException
  HResult=0x80070057
  Message=Keyword not supported: 'provider'.
  Source=System.Data.SqlClient

Open in new window

Would you know how i can fix this problem?

Thank you for your help.
Avatar of Alex [***Alex140181***]
Alex [***Alex140181***]
Flag of Germany image

What about
Dim oradb As String = "Data Source=orcl;User Id=hr;Password=hr;"
Dim conn As New OracleConnection(oradb) 
conn.Open()

Open in new window

https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/2015/getting_started_vb_version/oracle_data_provider_for_dotnet_vb_version.html
Avatar of Wilder1626

ASKER

When using it, i get the error:
System.TypeLoadException: 'Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

Open in new window

I've installed below NuGets.
Do i need references?
User generated image
And i've imported below:
Imports System.Data.SqlClient
Imports OracleConnection = Oracle.ManagedDataAccess.Client.OracleConnection
Imports OracleCommand = Oracle.ManagedDataAccess.Client.OracleCommand
Imports OracleDataReader = Oracle.ManagedDataAccess.Client.OracleDataReader
I don't know if this can also help:
Error: Oracle.ManagedDataAccess.Client.OracleException: 'Connection request timed out'

on code:  conn.Open()
       Dim oradb As String = "Data Source=i2_DV791;User Id=jdatm_dev;Password=jdatm_dev;"
        Dim conn As New OracleConnection(oradb)
        conn.Open()

Open in new window


Error detail:
Oracle.ManagedDataAccess.Client.OracleException
  HResult=0x80004005
  Message=Connection request timed out
  Source=Oracle Data Provider for .NET, Managed Driver
  StackTrace:
   at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, OracleConnection connRefForCriteria, String affinityInstanceName, Boolean bForceMatch)
   at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, OracleConnection connRefForCriteria, String affinityInstanceName, Boolean bForceMatch)
   at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword, OracleConnection connRefForCriteria)
   at Oracle.ManagedDataAccess.Client.OracleConnection.Open()

Open in new window

User generated imageWhich DLLs have you references then?!

According to https://stackoverflow.com/questions/49159199/connect-to-oracle-database-in-dotnet-mvc-application
it might be the wrong ones...
ASKER CERTIFIED SOLUTION
Avatar of Alex [***Alex140181***]
Alex [***Alex140181***]
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it's kind of weird cause on my side, i dont see the folder References. I see Dependencies

User generated imageWhen i right click on it, i get
User generated image
Then if i click Project References...
User generated image
In the link you provided, it was mentioning: Oracle.ManagedDataAccess.Core.dll Oracle.ManagedDataAccess.Core.dll
May be the problem.
I'm getting the error when using:
Dim oradb As String = "Data Source=i2_DV791;User Id=jdatm_dev;Password=jdatm_dev;Pooling=False" Dim conn As New OracleConnection(oradb) conn.Open()

Open in new window


System.TypeLoadException: 'Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

In the link you provided, it was mentioning: Oracle.ManagedDataAccess.Core.dll Oracle.ManagedDataAccess.Core.dll
May be the problem.                                   
I think so! Try to exchange those references!
Ok, i've followed the other link and i have uninstalled Oracle.ManagedDataAccess and only kept: Oracle.ManagedDataAccess.Core.

I have also removed from the PackageReference below:
Oracle.ManagedDataAccess

Open in new window


Now i just need to find: Oracle.ManagedDataAccess.Core.dll . I don't see it at all.

But now i have a new error:
Oracle.ManagedDataAccess.Client.OracleException: 'ORA-12154: TNS:could not resolve the connect identifier specified'


Open in new window

ORA-12154: TNS:could not resolve the connect identifier specified
Bingo ;-) We're getting closer :-))
Take a look at this, regarding your TNSNAMES.ORA (whereas you may also use a TNS-less connection string there):
https://stackoverflow.com/questions/31564288/connecting-to-oracle-using-oracle-manageddataaccess

Great ;-) no more error. Thanks for your guidance. So appreciated.

Now i just need to feed the DataGridView1 with the data. That portion of the code
   Dim da As New SqlDataAdapter("Select * from Location", conn)
        Dim dt As New DataTable


        da.Fill(dt)
        DataGridView1.DataSource = dt

Open in new window


I'm now having an error on SqlDataAdapter
User generated image
I think, you still have some (brain) bugs involved in the actual code ;-) *just kidding*
Take a look at the examples shown here:
https://docs.oracle.com/cd/E18283_01/appdev.112/e10767/building_odp.htm
Especially this part:
conn.Open() connect.Enabled = False

Dim sql As String = "select * from departments where department_id < 60"
cmd = New OracleCommand(sql, conn)
cmd.CommandType = CommandType.Text

da = New OracleDataAdapter(cmd)
cb = New OracleCommandBuilder(da)
ds = New DataSet()

da.Fill(ds)

departments.DataSource = ds.Tables(0)

Open in new window

you still have some (brain) bugs involved in the actual code
Yes i do. Ha ha ha

I actually added to the Public class
 Dim conn As New OracleConnection
    Private cmd As OracleCommand
    Private da As OracleDataAdapter
    Private cb As OracleCommandBuilder
    Private ds As DataSet

Open in new window


Then, on the Click button, i've added below code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim oradb As String = "Data Source=DataSource;User Id=UserId;Password=Password;"
        Dim conn As New OracleConnection(oradb)
Dim connect As Object = Nothing

        Try

            conn.Open()
            connect.Enabled = False

            Dim sql As String = "Select * from Location"

            cmd = New OracleCommand(sql, conn)
            cmd.CommandType = CommandType.Text

            da = New OracleDataAdapter(cmd)
            cb = New OracleCommandBuilder(da)
            ds = New DataSet()

            da.Fill(ds)

            DataGridView1.DataSource = ds.Tables(0)

        Catch ex As Exception

        End Try


    End Sub

Open in new window


I started to get the error on :da = New OracleDataAdapter(cmd)
(field) Form1.cmd AsOracleCommand
BC30311: Value of type 'OracleCommand' cannot be converted to  'OracleCommand'

From what i read on the link, I should not have to do anything else, if i did read it correctly. I will review again, just in case.
omg. It's getting worst.
Now i'm at below error. ha ha ha
System.InvalidOperationException: 'Application-defined or object-defined error.'

1 of 2 Inner Exceptions:
TypeInitializationException: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception.

2 of 2 Inner Exceptions:
OracleException: The provider is not compatible with the version of Oracle client




i'm starting to think that i should try to re-install Oracle 11g
Don't get me wrong, but you cannot just Copy&Paste this stuff into your code without adopting!!
You will never get a real Oracle DB connection with these values!
The other problem is setting the "connect" object to "Nothing" and trying to access its non-existing propery "Enabled" afterwards!

User generated image
I'm going to take a step back and try to figure out what's going on. I've tried so many things with no good results that I may be a little mixt-up right now. vb.net is pretty new to me as well.

But thanks for your help.
No problem ;-)
Never make to many changes at once! Just one step at a time (at least in this case)...
thanks for the advice. I will definitely follow your recommendation and take it one step at the time. :-)