Introduction

In this post we will discuss how to fetch data from Oracle database using C#. we will OracleDataReader . Before proceeding further I would suggest to go through Connecting To Oracle Database Using C#.

Go through the below code.

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Fetch Data From Oracle DataBase</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:GridView ID="grid" runat="server" ></asp:GridView>
  11. <asp:Button ID="btn_fetch" runat="server" Text="Fetch Data" OnClick="btn_fetch_Click" />
  12. </div>
  13. </form>
  14. </body>
  15. </html>

Default.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Oracle.DataAccess.Client;
  8. using System.Data;
  9. public partial class Default2 : System.Web.UI.Page
  10. {
  11. //creating TNS entries
  12. string oradb = "Data Source=(DESCRIPTION =" +
  13. "(ADDRESS = (PROTOCOL = TCP)(HOST = your_host_name)(PORT = 1521))" +
  14. "(CONNECT_DATA =" +
  15. "(SERVER = DEDICATED)" +
  16. "(SERVICE_NAME = XE)));" +
  17. "User Id=your_user_id;Password=*******;";
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. }
  21. protected void btn_fetch_Click(object sender, EventArgs e)
  22. {
  23. OracleConnection con = new OracleConnection(oradb);
  24. OracleCommand cmd = new OracleCommand();
  25. cmd.CommandText="select * from student";
  26. cmd.Connection = con;
  27. con.Open();
  28. OracleDataReader dr = cmd.ExecuteReader();
  29. if (dr.HasRows)
  30. {
  31. Response.Write("<table border='1'>");
  32. Response.Write("<tr><th>Name</th><th>Roll No</th></tr>");
  33. while (dr.Read())
  34. {
  35. Response.Write("<tr>");
  36. Response.Write("<td>" + dr["name"].ToString() + "</td>");
  37. Response.Write("<td>" + dr["roll_no"].ToString() + "</td>");
  38. Response.Write("</tr>");
  39. }
  40. Response.Write("</table>");
  41. }
  42. else
  43. {
  44. Response.Write("No Data In DataBase");
  45. }
  46. con.Close();
  47. }
  48. }

Have a look at below code.

code

  1. OracleConnection(): Initializes a new instance of the OracleConnection.

  2. OracleConnection(oradb): Initializes a new instance of the OracleConnection class with the specified connection string.

  3. OracleCommand(): Initializes a new instance of the OracleCommand.

  4. CommandText: Gets or sets the SQL statement or stored procedure to execute against the database. (Overrides DbCommand.CommandText.).

  5. Connection: Gets or sets the OracleConnection used by this instance of the OracleCommand.

  6. OracleDataReader: To create an OracleDataReader, you must call the ExecuteReader method of the OracleCommand object, rather than directly using a constructor. Changes made to a resultset by another process or thread while data is being read may be visible to the user of the OracleDataReader.

Output

output