Saturday, November 26, 2011

How to Read Oracle Database from Visual Basic .Net

Visual Basic .Net 2010 sample on how to connect and read data from Oracle Server.
Before starting this tutorial, you should install Microsoft Visual Studio 2010, Install Oracle Database 9.2 or later or Oracle Database XE and install Oracle 11g Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio version or later from OTN.

'Add the following VB.NET Imports statements before the Public Class declaration.

Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim oradb As String = "Data Source=orcl;User Id=hr;Password=hr;"
    Dim conn As New OracleConnection(oradb)
    Dim cmd As New OracleCommand
    cmd.Connection = conn
    cmd.CommandText = "select dept_name from departments where dept_id = 80"
    cmd.CommandType = CommandType.Text
    Dim dr As OracleDataReader = cmd.ExecuteReader()
    Text1.Text = dr.Item("dept_name")

End Sub


Post a Comment

Powered by Blogger