Free Information Technology Magazines and eBooks

Tuesday, June 23, 2009

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.


Imports System.Data
Imports System.Data.OracleClient


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

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

'Create connection
Dim con As New OracleConnection(connString)

Try
con.ConnectionString = connString
con.Open()
Console.WriteLine("Connected to Oracle.")

Catch ex As OracleException
Console.WriteLine("Error: " & ex.ToString())
Finally
' Close Connection
con.Close()
Console.WriteLine("Connection Closed")

End Try

End Sub


For connecting to SQL, Read the following article/s.



For more VB.NET coding tips & tricks, subscribe now.

0 comments: