VB.NET issue connecting to Oracle 11g

做~自己de王妃 提交于 2019-12-12 03:58:33

问题


OS: Windows 7 64bit
VB: Visual Studio 2010
Oracle Client: 11g

I am developing an application that connects to an Oracle 11g server. I am able to connect to the Oracle server via Oracle SQL Developer, ODBC (in SYSWOW64), and in VB Server Explorer.

I am using the .NET reference Oracle.DataAccess located at: C:\Oracle\product\11.2.0\client_32\ODP.NET\bin\2.x\Oracle.DataAccess.dll

I have tried adding the .DLL files to my bin folder as suggested by other questions and that does not work.

Does anyone have any other suggestions?

When I run my application, I get the following:

Here are the details:

System.TypeInitializationException was unhandled
  Message=The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception.
  Source=Oracle.DataAccess
  TypeName=Oracle.DataAccess.Client.OracleConnection
  StackTrace:
       at Oracle.DataAccess.Client.OracleConnection..ctor(String connectionString)
       at Orc_Test_1.Form1.Button1_Click(Object sender, EventArgs e) in c:\Visual Studio 2010\Projects\Orc_Test_1\Orc_Test_1\Form1.vb:line 9
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Orc_Test_1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Oracle.DataAccess.Client.OracleException
       DataSource=""
       ErrorCode=-2147467259
       Message=The provider is not compatible with the version of Oracle client
       Number=-11
       Procedure=""
       Source=Oracle Data Provider for .NET
       StackTrace:
            at Oracle.DataAccess.Client.OracleInit.Initialize()
            at Oracle.DataAccess.Client.OracleConnection..cctor()
       InnerException: 

VB.NET Code:

Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oradb As String = "Data Source=SERVER1;Persist Security Info=True;User ID=username;Password=password;Unicode=True" 'From Server Explorer
        'Dim oradb As String = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=Server1.host.net)(PORT=1522))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=Server1))); USER ID = username;Password = password;" 'From TNSnames.ora
        Dim conn As New OracleConnection(oradb)  '<--- Error happens on this line
        conn.Open()
        Dim cmd As New OracleCommand
        cmd.Connection = conn
        cmd.CommandText = "select system_id from schema1.system_impacted where system_name = AWESOME"
        cmd.CommandType = CommandType.Text
        Dim dr As OracleDataReader = cmd.ExecuteReader()
        dr.Read()
        Label1.Text = dr.Item("system_id")
        conn.Dispose()
    End Sub
End Class

回答1:


I feel your pain, just went through something similar in a deployment situation. You probably have multiple clients installed, and your environment is pulling dlls for older releases (even if you have a latest oracle.dataaccess.dll correctly referenced in your project). Fixing this on your dev environment is one thing, a prod deployment server is another. Not sure what your deployment situation is, but here's what worked for me.

After struggling with trying to upgrade odp.net in existing oracle home, adding new oracle home, etc., I found the easiest way to fix everything is to download the latest odac with xcopy deployment from Oracle, and follow the readme (and see here for an older article on this also). Basically you'll run an install.bat file to setup locally (in separate folder, mine was c:\oracle_odac), then change your project reference to point to the oracle.dataaccess.dll in this new folder (I used 4 instead of 2.x), and add the new folder's bin dirs to front of your path (c:\oracle_odac\bin and c:\oracle_odac\odp.net\bin\4). On your deployment server, you'll just need to copy the entire c:\oracle_odac folder over (via xcopy or however), and setup the path.

That said, I anxiously await the production release of the fully managed odp.net from Oracle (in beta now).

EDIT: Just to add that you can avoid messing with PATHs if you setup in your app or web config file the dllpath. For example:

<configuration>
...
    <configSections>
      <section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>  
...

<oracle.dataaccess.client>
    <settings>
      <add name="DllPath" value="c:\oracle_odac\bin"/>
    </settings>
  </oracle.dataaccess.client>
...

This will override other settings such as registry or machine.config. And it will allow multiple odp.net configurations to exists peacefully, and allow each app to point to the version it needs on the same server.




回答2:


Usually happens when you're building x64 application which is trying to pick up x32 drivers that doesn't really work.

What you do in this case (and it's quite painful):

  • Install x32 oracle client to oraclehome1
  • Install x64 oracle client to oraclehome2

Make sure your application build (x64, x32) is pointed to correct oraclehome. I don't have that code handy, but I believe it is something along the lines of "Home=oraclehome2" in the connection string or similar.




回答3:


I removed all of the oracle clients that were available in the 'Programs' section of 'Control Panel'. Then I went into the registry searched for 'Oracle' and removed all instances/references of Oracle client(s). After 3 hours of registry removal and a reboot, I installed the 11g client and everything worked as expected.

Note: when searching the registry for "Oracle", there may be references to the VB version of Oracle, or a reference to Crytsal Reports driver for 'Oracle'. I did not remove these. So pay attention to the file location and/or the syntax of the registry item you are deleting.

/Reading. It works.




回答4:


download ODAC 32/ ODAC 64 bit ,depending upon on the compatibility of OS, VS and oracle 11g . Use oracle 11g 32,os 32 bit,oracle 32 bit or oracle 64 bit,os 64 bit and oracle 11g 64 bit.




回答5:


In my experience, after trying tbone's suggestion, playing with PATH env variable and other unsuccessful plays, the solution that worked immediately was to save a copy of the 8 DLLs files listed below RIGHT ON THE SAME FOLDER of the vb.net executable, and then add a reference to Oracle.DataAccess.dll in the same folder.

C:\Users\Legarcia\Documents\Visual Studio 2010\Projects\LabRetention\LabRetention\bin\Debug>dir *.dll
 Volume in drive C is OS
 Volume Serial Number is 10EC-8C5D

 Directory of C:\Users\Legarcia\Documents\Visual Studio 2010\Projects\LabRetention\LabRetention\bin\Debug

09/30/2008  07:57 PM           348,160 msvcr71.dll
10/01/2008  02:22 AM           520,192 oci.dll
10/01/2008  01:56 AM           352,256 ociw32.dll
08/12/2009  07:13 AM         1,011,712 Oracle.DataAccess.dll
09/18/2008  08:47 PM         1,130,496 orannzsbb11.dll
10/01/2008  01:48 AM           868,352 oraocci11.dll
10/01/2008  02:45 AM       117,604,352 oraociei11.dll
08/12/2009  07:21 AM           413,696 OraOps11w.dll
               8 File(s)    122,249,216 bytes
               0 Dir(s)  238,127,943,680 bytes free

When developing/testing/debuging, the set of DLLs is copied to ..\projects\{Your project}\bin\Debug

e.g.: [C:\Users\Legarcia\Documents\Visual Studio 2010\Projects\LabRetention\LabRetention\bin\Debug]

And when releasing the application for the users just copy the DLLs to the same dir of your EXE

This seems to isolate all other Ora client installations.

The connection string used in the VB.net app is created from the info in TNSNAMES.ORA file found on some other client directory.

Regards.

LE+



来源:https://stackoverflow.com/questions/12977790/vb-net-issue-connecting-to-oracle-11g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!