使用控制台应用程序时,"OraOLEDB.Oracle"提供程序未在本地计算机上注册? [英] The 'OraOLEDB.Oracle' provider is not registered on the local machine, when working with a console application?

查看:188
本文介绍了使用控制台应用程序时,"OraOLEDB.Oracle"提供程序未在本地计算机上注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

ConnectionString属性尚未初始化.

The ConnectionString property has not been initialized.

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

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

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

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

我不知道为什么,相同的连接现在可以在我的其他项目上工作.

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

我的连接类别:

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

当我称呼它时:

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

推荐答案

问题源于以下事实:在64位计算机上启动时,为AnyCPU编译的代码将由JIT生成为64位代码,因此无法使用32位驱动程序.只需重新编译x86平台的代码,即可使其在32位和64位系统上运行良好,并使用32位驱动程序.

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.

第二个问题是由于您没有调用connect()而导致的,因此您不应该使用该connection.con对象,更不用说对其进行打开了

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

让我在上面重写您的一些代码

Let me rewrite some of your code above

首先,应将类更改为

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

如您所见,当您调用GetConnection()时,该类将返回正确初始化的连接,并且您可以在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.

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

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....

这篇关于使用控制台应用程序时,"OraOLEDB.Oracle"提供程序未在本地计算机上注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆