Read decimal from OracleDataReader : Oracle « ADO.Net « C# / CSharp Tutorial






using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OracleClient;

  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button btnConnect;
    private System.Windows.Forms.Button btnGetIDs;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ComboBox cbEmpIds;
    private System.Windows.Forms.Label lblFirstName;
    private System.Windows.Forms.Label lblLastName;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Button btnLookup1;
    private System.Windows.Forms.Button btnLookup2;
    private System.Windows.Forms.Label lblEmail;
    private System.Windows.Forms.Label lblPhone;
    private System.Windows.Forms.Label lblEmailText;
    private System.Windows.Forms.Label lblPhoneText;
    private System.Windows.Forms.Button btnReset;
    private System.Windows.Forms.Button btnNoBinds;
    private System.Data.OracleClient.OracleConnection oraConn;
    private System.Data.OracleClient.OracleCommand cmdGetIDs;
    private System.Data.OracleClient.OracleCommand cmdLookup1;
    private System.Data.OracleClient.OracleCommand cmdLookup2;
    private System.Data.OracleClient.OracleCommand cmdNoBinds1;
    private System.Data.OracleClient.OracleCommand cmdNoBinds2;
    public Form1()
    {
      this.btnConnect = new System.Windows.Forms.Button();
      this.btnGetIDs = new System.Windows.Forms.Button();
      this.cbEmpIds = new System.Windows.Forms.ComboBox();
      this.label1 = new System.Windows.Forms.Label();
      this.lblFirstName = new System.Windows.Forms.Label();
      this.lblLastName = new System.Windows.Forms.Label();
      this.label4 = new System.Windows.Forms.Label();
      this.label5 = new System.Windows.Forms.Label();
      this.btnLookup1 = new System.Windows.Forms.Button();
      this.btnLookup2 = new System.Windows.Forms.Button();
      this.lblEmail = new System.Windows.Forms.Label();
      this.lblPhone = new System.Windows.Forms.Label();
      this.lblEmailText = new System.Windows.Forms.Label();
      this.lblPhoneText = new System.Windows.Forms.Label();
      this.btnReset = new System.Windows.Forms.Button();
      this.btnNoBinds = new System.Windows.Forms.Button();
      this.oraConn = new System.Data.OracleClient.OracleConnection();
      this.cmdGetIDs = new System.Data.OracleClient.OracleCommand();
      this.cmdLookup1 = new System.Data.OracleClient.OracleCommand();
      this.cmdLookup2 = new System.Data.OracleClient.OracleCommand();
      this.cmdNoBinds1 = new System.Data.OracleClient.OracleCommand();
      this.cmdNoBinds2 = new System.Data.OracleClient.OracleCommand();
      this.SuspendLayout();
      this.btnConnect.Location = new System.Drawing.Point(32, 44);
      this.btnConnect.Text = "C&onnect";
      this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
      this.btnGetIDs.Location = new System.Drawing.Point(32, 80);
      this.btnGetIDs.Text = "&Get IDs";
      this.btnGetIDs.Click += new System.EventHandler(this.btnGetIDs_Click);
      this.cbEmpIds.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.cbEmpIds.Location = new System.Drawing.Point(144, 44);
      this.cbEmpIds.Size = new System.Drawing.Size(68, 21);
      this.label1.Location = new System.Drawing.Point(144, 24);
      this.label1.Size = new System.Drawing.Size(72, 16);
      this.label1.Text = "&Employee ID:";
      this.lblFirstName.Location = new System.Drawing.Point(228, 48);
      this.lblFirstName.Size = new System.Drawing.Size(116, 16);
      this.lblLastName.Location = new System.Drawing.Point(364, 48);
      this.lblLastName.Size = new System.Drawing.Size(128, 16);
      this.label4.Location = new System.Drawing.Point(228, 24);
      this.label4.Size = new System.Drawing.Size(100, 16);
      this.label4.Text = "&First Name:";
      this.label5.Location = new System.Drawing.Point(364, 24);
      this.label5.Size = new System.Drawing.Size(100, 16);
      this.label5.Text = "&Last Name:";
      this.btnLookup1.Location = new System.Drawing.Point(32, 116);
      this.btnLookup1.Text = "Lookup &1";
      this.btnLookup1.Click += new System.EventHandler(this.btnLookup1_Click);
      this.btnLookup2.Location = new System.Drawing.Point(32, 152);
      this.btnLookup2.Text = "Lookup &2";
      this.btnLookup2.Click += new System.EventHandler(this.btnLookup2_Click);
      this.lblEmail.Location = new System.Drawing.Point(228, 88);
      this.lblEmail.Size = new System.Drawing.Size(100, 16);
      this.lblEmail.Text = "E&mail:";
      this.lblPhone.Location = new System.Drawing.Point(364, 88);
      this.lblPhone.Size = new System.Drawing.Size(100, 16);
      this.lblPhone.Text = "Phone &Number:";
      this.lblEmailText.Location = new System.Drawing.Point(228, 112);
      this.lblEmailText.Size = new System.Drawing.Size(116, 16);
      this.lblPhoneText.Location = new System.Drawing.Point(364, 112);
      this.lblPhoneText.Size = new System.Drawing.Size(128, 16);
      this.btnReset.Location = new System.Drawing.Point(32, 224);
      this.btnReset.Text = "&Reset";
      this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
      this.btnNoBinds.Location = new System.Drawing.Point(32, 188);
      this.btnNoBinds.Text = "No &Binds";
      this.btnNoBinds.Click += new System.EventHandler(this.btnNoBinds_Click);
      this.oraConn.ConnectionString = "user id=hr;data source=oranet;password=demo";
      this.cmdGetIDs.CommandText = "SELECT EMPLOYEE_ID FROM EMPLOYEES ORDER BY EMPLOYEE_ID";
      this.cmdGetIDs.Connection = this.oraConn;
      this.cmdLookup1.CommandText = "SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEES WHERE (EMPLOYEE_ID = :p_id)";
      this.cmdLookup1.Connection = this.oraConn;
      this.cmdLookup1.Parameters.Add(new System.Data.OracleClient.OracleParameter(":p_id", System.Data.OracleClient.OracleType.Number, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(6)), ((System.Byte)(0)), "EMPLOYEE_ID", System.Data.DataRowVersion.Current, null));
      this.cmdLookup2.CommandText = "SELECT EMAIL, PHONE_NUMBER FROM EMPLOYEES WHERE (FIRST_NAME = :p_first) AND (LAST" +
        "_NAME = :p_last)";
      this.cmdLookup2.Connection = this.oraConn;
      this.cmdLookup2.Parameters.Add(new System.Data.OracleClient.OracleParameter(":p_first", System.Data.OracleClient.OracleType.VarChar, 20, "FIRST_NAME"));
      this.cmdLookup2.Parameters.Add(new System.Data.OracleClient.OracleParameter(":p_last", System.Data.OracleClient.OracleType.VarChar, 25, "LAST_NAME"));
      this.cmdNoBinds1.Connection = this.oraConn;
      this.cmdNoBinds2.Connection = this.oraConn;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(534, 264);
      this.Controls.Add(this.btnNoBinds);
      this.Controls.Add(this.btnReset);
      this.Controls.Add(this.lblPhoneText);
      this.Controls.Add(this.lblEmailText);
      this.Controls.Add(this.lblPhone);
      this.Controls.Add(this.lblEmail);
      this.Controls.Add(this.btnLookup2);
      this.Controls.Add(this.btnLookup1);
      this.Controls.Add(this.label5);
      this.Controls.Add(this.label4);
      this.Controls.Add(this.lblLastName);
      this.Controls.Add(this.lblFirstName);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.cbEmpIds);
      this.Controls.Add(this.btnGetIDs);
      this.Controls.Add(this.btnConnect);
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void btnConnect_Click(object sender, System.EventArgs e)
    {
      if (oraConn.State != ConnectionState.Open)
      {
         oraConn.Open();
         MessageBox.Show(oraConn.ConnectionString, "Successful Connection");
      }
    }

    private void btnGetIDs_Click(object sender, System.EventArgs e)
    {
        OracleDataReader dataReader = cmdGetIDs.ExecuteReader();
        while (dataReader.Read())
        {
          cbEmpIds.Items.Add(dataReader.GetDecimal(0));
        }
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
    }

    private void btnLookup1_Click(object sender, System.EventArgs e)
    {
      object selectedItem = cbEmpIds.SelectedItem;

      if (selectedItem != null)
      {
        cmdLookup1.Parameters[0].Value = Convert.ToDecimal(selectedItem.ToString());
        OracleDataReader dataReader = cmdLookup1.ExecuteReader();
        if (dataReader.Read())
        {
          lblFirstName.Text = dataReader.GetString(0);
          lblLastName.Text = dataReader.GetString(1);
        }

        dataReader.Close();
        dataReader.Dispose();
      }
    }

    private void btnLookup2_Click(object sender, System.EventArgs e)
    {
      cmdLookup2.Parameters[0].Value = lblFirstName.Text;
      cmdLookup2.Parameters[1].Value = lblLastName.Text;
      OracleDataReader dataReader = cmdLookup2.ExecuteReader();
      if (dataReader.Read())
      {
        lblEmailText.Text = dataReader.GetString(0);
        lblPhoneText.Text = dataReader.GetString(1);
      }

      dataReader.Close();

      dataReader.Dispose();
    }

    private void btnReset_Click(object sender, System.EventArgs e)
    {
      cbEmpIds.SelectedIndex = -1;
      lblFirstName.Text = "";
      lblLastName.Text = "";
      lblEmailText.Text = "";
      lblPhoneText.Text = "";
    }

    private void btnNoBinds_Click(object sender, System.EventArgs e)
    {
      object selectedItem = cbEmpIds.SelectedItem;

      if (selectedItem != null)
      {
        OracleDataReader dataReader;

        cmdNoBinds1.CommandText = "select first_name, last_name from employees where employee_id = " + selectedItem.ToString();
        dataReader = cmdNoBinds1.ExecuteReader();
        if (dataReader.Read())
        {
          lblFirstName.Text = dataReader.GetString(0);
          lblLastName.Text = dataReader.GetString(1);
        }
        dataReader.Close();
        cmdNoBinds2.CommandText = "select email, phone_number from employees where first_name = '" + lblFirstName.Text + "' and last_name = '" + lblLastName.Text +"'";
        dataReader = cmdNoBinds2.ExecuteReader();
        if (dataReader.Read())
        {
          lblEmailText.Text = dataReader.GetString(0);
          lblPhoneText.Text = dataReader.GetString(1);
        }

        dataReader.Close();
        dataReader.Dispose();
      }
    }
  }








32.3.Oracle
32.3.1.how to use an OleDbConnection object to connect to an Oracle database
32.3.2.A connection string using integrated security for Oracle database
32.3.3.A connection string without integrated security for Oracle database
32.3.4.Get connection state and server version
32.3.5.Using OracleCommand to do query
32.3.6.Using OracleCommand to do the query sql
32.3.7.Using OracleCommandBuilder
32.3.8.Using OracleDataAdapter
32.3.9.Using OracleDataReader to read from Oracle database
32.3.10.Using OracleParameter
32.3.11.Connect to an Oracle Database using .NET data provider for OLE DB
32.3.12.Connect to an Oracle Database using OdbcConnection
32.3.13.Connecting to an Oracle Database with OracleConnection
32.3.14.Do a delete command to Oracle database
32.3.15.Do an insert command to Oracle database
32.3.16.Do an update to Oracle database
32.3.17.Manual Loopup for Oracle database
32.3.18.Read string from OracleDataReader
32.3.19.Use OracleConnection to connect to Oracle database
32.3.20.Rollback for Oracle database
32.3.21.Runs the CustomerAdd stored procedure.
32.3.22.No Connection Pooling
32.3.23.Read decimal from OracleDataReader
32.3.24.Connection String for Oracle database