vb.net 使用控制台应用程序时,“OraOLEDB.Oracle”提供程序未在本地计算机上注册?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14566687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:04:07  来源:igfitidea点击:

The 'OraOLEDB.Oracle' provider is not registered on the local machine, when working with a console application?

databasevb.netoracle10gconsole-application

提问by user1676874

I have this website application I've been working on, VB.NET .NET 3.5, everything works fine. I also need to develop a console application that updates the website's database. I use an Oracle 10g database.

我有我一直在开发的这个网站应用程序,VB.NET .NET 3.5,一切正常。我还需要开发一个控制台应用程序来更新网站的数据库。我使用 Oracle 10g 数据库。

I copied the same connection class I use on my main project, when I try to call the connection method I get this error:

我复制了我在主项目中使用的相同连接类,当我尝试调用连接方法时出现此错误:

The ConnectionString property has not been initialized.

ConnectionString 属性尚未初始化。

Or this error if I don't use the class and call the code directly:

或者,如果我不使用该类并直接调用代码,则会出现此错误:

The 'OraOLEDB.Oracle' provider is not registered on the local machine

“OraOLEDB.Oracle”提供程序未在本地计算机上注册

I have no idea why, the same connection works right now on my other project.

我不知道为什么,同样的连接现在在我的另一个项目上工作。

My connection class:

我的连接类:

Public Class connection
 Public con As New OleDbConnection
 Public Sub connect()
  con = New OleDbConnection
  con.ConnectionString = "Data Source=localhost;User Id=system;Password=root;Provider=OraOLEDB.Oracle"
 End Sub
End Class

And when I call it:

当我调用它时:

connection.con.Open()
 sql.Connection = connection.con
 sql.CommandText = ...
 sql.CommandType = CommandType.Text
 reader = sql.ExecuteReader()
 While (reader.Read())
  ...
 End While
connection.con.Close()

采纳答案by Steve

The problem stems from the fact that a code compiled for AnyCPU when is started on a 64 bit machine will be generated by the JIT as 64bit code and thus cannot use 32bit drivers. Simply recompiling the code for x86 platform will allow it to run as well on 32bit and 64bit systems and use 32bit drivers.

问题源于这样一个事实,即在 64 位机器上启动时为 AnyCPU 编译的代码将由 JIT 生成为 64 位代码,因此不能使用 32 位驱动程序。简单地重新编译 x86 平台的代码将允许它在 32 位和 64 位系统上运行,并使用 32 位驱动程序。

The second problem instead is caused by the fact that you don't call connect() and thus you should not be able to use that connection.con object, much less call open on it

第二个问题是由于您没有调用 connect() 并且因此您不应该使用该 connection.con 对象,更不用说在其上调用 open

Let me rewrite some of your code above

让我重写你上面的一些代码

First, the class should be changed to

首先,类应该改为

Public Class Connection
    Private con As OleDbConnection
    Public OleDbConnection GetConnection()
        con = New OleDbConnection
        con.ConnectionString = "Data Source=localhost;" + 
                                "User Id=system;Password=root;Provider=OraOLEDB.Oracle"
        return con
    End Function
End Class

now the code could call the above class in this way

现在代码可以通过这种方式调用上面的类

Connection cc = new Connection()
Using con = cc.GetConnection()
    con.Open()
    sql.Connection = con
    sql.CommandText = ...
    sql.CommandType = CommandType.Text
    reader = sql.ExecuteReader()
    While (reader.Read())
        ...
    End While
End Using

As you can see the class returns a correctly initialized connection when you call GetConnection() and you can use this object in a Using statement that serves the purpose of releasing the resources used by the connection when there is no more need of it.

正如您所看到的,当您调用 GetConnection() 时,该类返回一个正确初始化的连接,您可以在 Using 语句中使用此对象,该语句用于在不再需要连接时释放连接使用的资源。

Of course the Class Connection itself is pretty useless as is because you could write a simpler code directly on the code that need a connection without hiding the creation of the connection.
But if you plan to add more functionality....

当然,类连接本身是非常无用的,因为您可以直接在需要连接的代码上编写更简单的代码,而无需隐藏连接的创建。
但是,如果您打算添加更多功能......

回答by Pellizon

Try to set your application debugging for x86 OS (32-bits)

尝试为 x86 操作系统(32 位)设置应用程序调试

Build -> Configuration Manager -> Debug -> Configuration:Debug -> Platform x86

构建 -> 配置管理器 -> 调试 -> 配置:调试 -> 平台 x86