In this article, we will learn how to connect oracle database in using c#, so for this, you will need following Visual Studio 2012 or Higher, I am using Visual Studio 2017 community, other than this, download these two files from oracle to support Oracle database in .NET.

  1. Oracle Developers tools for .NET :
  2. Oracle Database (I hope you alread have this, if not download it from here

Once you have all the prerequisite, we can proceed to next steps.

Step 1: Create a new ASP.NET Empty web-application project in your Visual Studio, by navigating to File -> New -> Project -> Select "ASP.NET web-application (Right pane)" & "Web (Left-Pane)" -> Name your project "OracleInASPNET" -> Click "OK" -> Select "Empty"  project temaplet and Click "OK"

Step 2: Create a Web-Form named as "Default.aspx" (Right-click Solution, Click "Add"-> Select "Web-Form" from options )

Step 3: Add a GridView and a button in the above created web-form, as show below

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="OracleInASPNET.Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server">
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Step 4: Now we will add Oracle assemblies in our project, so right-click on "reference" and then add the following dll, as shown in the image below


Once you have added the DLL refernce, you need to Find "HostName" if your Oracle Database, so here is the Command for it

select sys_context('USERENV','SERVER_HOST') from dual

I was already connected to database to I used this command

SELECT  host_name
FROM    v$instance


And here is my database, for which we will fetch records and display in the GridView


So, the Database name = "Books" (Service_name) and table name is "BooksTable".

Step 5: once you have got all the details like Host_Name, Service Name, table name and password, you can now add the C# code to fetch the records in Datatable and fill in GridView to display it, here is the C# code for "default.aspx.cs"

using System;
using System.Data;
using System.Data.OracleClient;

namespace OracleInASPNET
    public partial class Default : System.Web.UI.Page
        string oradb = "Data Source=(DESCRIPTION =" +
            "(ADDRESS = (PROTOCOL = TCP)(HOST = DESKTOP-4K41ID9)(PORT = 1521))" + 
            "(CONNECT_DATA =" + "(SERVER = DEDICATED)" + 
            "(SERVICE_NAME = books)));" + 
            "User Id=SYSTEM;Password=YourPassword;";
        protected void Page_Load(object sender, EventArgs e)


        protected void Button1_Click(object sender, EventArgs e)
            OracleConnection Con = new OracleConnection(oradb);
            DataTable tab = new DataTable();
            OracleDataAdapter da = new OracleDataAdapter("select * from BOOKSTABLE", Con);
            GridView1.DataSource = tab;

You might also like to read:

SQL server connection string examples in C# & XML

Creating C# Custom Exception (With Console application example)

Now, you can build and execute it in browser, you will see output as below


Note: It is better to download and use SQL developer than SQL Plus CLI command for .NET developer, as .NET developer usually use SQL server management and Oracle SQL develoepr is similar tool

That's it, we are done.