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 11.2.0.1.2 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)
    conn.Open()
    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()
    dr.Read()
    Text1.Text = dr.Item("dept_name")
    conn.Dispose()


End Sub


0 comments:

Post a Comment

 
Powered by Blogger