连接到Oracle 11g的VB.NET问题
我正在开发一个连接到Oracle 11g服务器的应用程序。我能够通过Oracle SQL Developer、ODBC(在SYSWOW64中)和VB服务器资源管理器连接到Oracle服务器 我使用的是.NET引用Oracle.DataAccess,位于:C:\Oracle\product\11.2.0\client\u 32\ODP.NET\bin\2.x\Oracle.DataAccess.dll 我已尝试按照其他问题的建议将.DLL文件添加到我的bin文件夹中,但不起作用 有人有其他建议吗 当我运行我的应用程序时,我得到以下信息: 详情如下:连接到Oracle 11g的VB.NET问题,vb.net,oracle11g,vb.net-2010,odp.net,Vb.net,Oracle11g,Vb.net 2010,Odp.net,我正在开发一个连接到Oracle 11g服务器的应用程序。我能够通过Oracle SQL Developer、ODBC(在SYSWOW64中)和VB服务器资源管理器连接到Oracle服务器 我使用的是.NET引用Oracle.DataAccess,位于:C:\Oracle\product\11.2.0\client\u 32\ODP.NET\bin\2.x\Oracle.DataAccess.dll 我已尝试按照其他问题的建议将.DLL文件添加到我的bin文件夹中,但不起作用 有人有其他建议吗
OS: Windows 7 64bit
VB: Visual Studio 2010
Oracle Client: 11g
VB.NET代码:
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:
导入系统数据
导入Oracle.DataAccess.Client的ODP.NET Oracle托管提供程序
导入Oracle.DataAccess.Types
公开课表格1
私有子按钮1\u单击(发送者作为System.Object,e作为System.EventArgs)处理按钮1。单击
从服务器资源管理器中将oradb设置为String=“Data Source=SERVER1;Persist Security Info=True;User ID=username;Password=Password;Unicode=True”
'Dim oradb As String=“数据源=(描述=(地址=(协议=TCP)(主机=服务器1.HOST.net)(端口=1522))(连接数据=(服务器=专用)(服务名称=服务器1));用户ID=用户名;密码=密码;”,来自TNSnames.ora
Dim conn As New OracleConnection(oradb)通常发生在您构建x64应用程序时,该应用程序试图获取实际无法工作的x32驱动程序
在这种情况下你会做什么(这很痛苦):
- 将x32 oracle客户端安装到oraclehome1
- 将x64 oracle客户端安装到oraclehome2
确保应用程序构建(x64、x32)指向正确的oraclehome。我手头没有这段代码,但我相信它是连接字符串中类似“Home=oraclehome2”的代码 通常发生在您构建x64应用程序时,该应用程序试图获取实际不起作用的x32驱动程序
在这种情况下你会做什么(这很痛苦):
- 将x32 oracle客户端安装到oraclehome1
- 将x64 oracle客户端安装到oraclehome2
确保应用程序构建(x64、x32)指向正确的oraclehome。我手头没有这段代码,但我相信它是连接字符串中类似“Home=oraclehome2”的代码 我感觉到你的痛苦,只是在部署情况下经历了类似的事情。您可能安装了多个客户机,并且您的环境正在为旧版本拉dll(即使您的项目中正确引用了最新的oracle.dataaccess.dll)。在开发环境中解决这一问题是一回事,而prod部署服务器则是另一回事。不确定你的部署情况是什么,但以下是对我有效的方法
在努力升级现有oracle home中的odp.net、添加新的oracle home等之后,我发现解决所有问题的最简单方法是从oracle下载最新版本,并遵循自述文件(有关这方面的旧文章,请参阅)。基本上,您将运行一个install.bat文件在本地进行安装(在单独的文件夹中,我的是c:\oracle\u odac),然后将项目引用更改为指向这个新文件夹中的oracle.dataaccess.dll(我使用的是4而不是2.x),并将新文件夹的bin dirs添加到路径前面(c:\oracle\u odac\bin和c:\oracle\u odac\odp.net\bin\4)。在部署服务器上,只需复制整个c:\oracle\u odac文件夹(通过xcopy或其他方式),然后设置路径
也就是说,我急切地等待着Oracle发布的产品版本(现在是beta版)
编辑:只是为了补充一点,如果在应用程序或web配置文件中设置dllpath,可以避免弄乱路径。例如:
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
...
...
...
这将覆盖注册表或machine.config等其他设置。它将允许多个odp.net配置和平存在,并允许每个应用程序指向同一服务器上所需的版本。我感觉到了你的痛苦,只是在部署情况下经历了类似的事情。您可能安装了多个客户机,并且您的环境正在为旧版本拉dll(即使您的项目中正确引用了最新的oracle.dataaccess.dll)。在开发环境中解决这一问题是一回事,而prod部署服务器则是另一回事。不确定你的部署情况是什么,但以下是对我有效的方法
在努力升级现有oracle home中的odp.net、添加新的oracle home等之后,我发现解决所有问题的最简单方法是从oracle下载最新版本,并遵循自述文件(有关这方面的旧文章,请参阅)。基本上,您将运行一个install.bat文件在本地进行安装(在单独的文件夹中,我的是c:\oracle\u odac),然后将项目引用更改为指向这个新文件夹中的oracle.dataaccess.dll(我使用的是4而不是2.x),并将新文件夹的bin dirs添加到路径前面(c:\oracle\u odac\bin和c:\oracle\u odac\odp.net\bin\4)。在部署服务器上,只需复制整个c:\oracle\u odac文件夹(通过xcopy或其他方式),然后设置路径
也就是说,我急切地等待着Oracle发布的产品版本(现在是beta版)
编辑:只是为了补充一点,如果在应用程序或web配置文件中设置dllpath,可以避免弄乱路径。例如:
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
...
...
...
这将覆盖注册表或machine.config等其他设置。它将允许多个odp.net配置和平存在,并允许每个应用程序指向同一服务器上所需的版本。我删除了“控制面板”的“程序”部分中可用的所有oracle客户端。然后我进入注册表搜索“Oracle”,删除了Oracle客户端的所有实例/引用。经过3个小时的注册表删除和重新启动,我安装了
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