问题
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:
The ConnectionString property has not been initialized.
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
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()
回答1:
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.
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
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.
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....
回答2:
Try to set your application debugging for x86 OS (32-bits)
Build -> Configuration Manager -> Debug -> Configuration:Debug -> Platform x86
来源:https://stackoverflow.com/questions/14566687/the-oraoledb-oracle-provider-is-not-registered-on-the-local-machine-when-work