In this tutorial, you will learn how to connect Oracle Database in VB.Net 2017 using ADO.NET data access provider.
Follow These Steps to Connect Oracle Database in VB.NET 2017
- First, download Oracle Data Access Components for .NET from Oracle with the following link: Download ODAC for Visual Studio 2017.
- After completing the download, install it on your system.
- Then in Visual Studio 2017, open your project or create a new and do right click on the Reference node in Solution Explorer.
- From the shortcut menu, choose Add Reference option and then in the Reference Manager window look for Oracle.ManagedDataAccess and check the check-box and click on OK button, as shown in the below image:
Now you are ready to use Oracle Data Access components in your VB.NET 2017 program and below is an example.
VB.NET 2017 Program Example to Connect Oracle Database
In the following example, it will ask the user to enter Employee's ID, and after clicking on the Query button, it will show the employee's first and last name using the MessageBox. The schema used for this example is HR.
Imports System.Data Imports Oracle.ManagedDataAccess.Client Imports Oracle.ManagedDataAccess.Types Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim oradb As String = "Data Source=localhost:1521/orcl;User Id=hr;Password=hrpsw;" Dim conn As New OracleConnection(oradb) conn.Open() Dim cmd As New OracleCommand Dim parm As New OracleParameter parm.OracleDbType = OracleDbType.Int64 parm.Value = TextBox1.Text cmd.Connection = conn cmd.Parameters.Add(parm) cmd.CommandText = "select first_name, last_name from employees where employee_id = :1" cmd.CommandType = CommandType.Text Try Dim dr As OracleDataReader = cmd.ExecuteReader() dr.Read() MessageBox.Show("Employee Name: " + dr.Item("first_Name") + " " + dr.Item("last_name"), "Employee Info") Catch ex As Exception MessageBox.Show(ex.Message, "Error") End Try conn.Dispose() End Sub End Class