VB.NET: How to connect to Oracle Database

Connecting to an Oracle Database server is almost the same as connecting to SQL Server and MySQL. The only noticeable difference is that the namespace to use is System.Data.OracleClient. The System.Data.OracleClient namespace is the .NET Framework Data Provider for Oracle that composed of a collection of classes for accessing an Oracle data source in the managed space.

On this Tutorial, I will show you a sample code on How to connect to a Oracle Database.

1. First we need to add reference to System.Data.OracleClient namespace by right-clicking your project on solution explorer then choose "Add Reference".

VB.NET: How to connect to Oracle Database

2. Include the following System.Data.OracleClient namespace on our code as shown below.

  1. Imports System.Data
  2. Imports System.Data.OracleClient

3. Then on your main function, add the code that connects to Oracle server.

  1. Sub Main()
  2. Dim connString As String = "server = oracleserver; uid = admin;password = password;"

  3. 'Create connection
  4. Dim con As New OracleConnection(connString)

  5. Try
  6. con.ConnectionString = connString
  7. con.Open()
  8. Console.WriteLine("Connected to Oracle.")

  9. Catch ex As OracleException
  10. Console.WriteLine("Error: " & ex.ToString())
  11. Finally
  12. ' Close Connection
  13. con.Close()
  14. Console.WriteLine("Connection Closed")

  15. End Try

  16. End Sub



Popular Posts