Using OracleDataAdapter : 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
  {
    public Form1()
    {
      this.btnConnect = new System.Windows.Forms.Button();
      this.btnClear = new System.Windows.Forms.Button();
      this.btnLoad = new System.Windows.Forms.Button();
      this.dgPlayerTable = new System.Windows.Forms.DataGrid();
      this.btnUpdate = new System.Windows.Forms.Button();
      this.btnBind = new System.Windows.Forms.Button();
      ((System.ComponentModel.ISupportInitialize)(this.dgPlayerTable)).BeginInit();
      this.SuspendLayout();
      this.btnConnect.Location = new System.Drawing.Point(16, 12);
      this.btnConnect.Text = "C&onnect";
      this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
      this.btnClear.Location = new System.Drawing.Point(16, 84);
      this.btnClear.Text = "&Clear Grid";
      this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
      this.btnLoad.Location = new System.Drawing.Point(16, 120);
      this.btnLoad.Text = "&Load Grid";
      this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
      this.dgPlayerTable.DataMember = "";
      this.dgPlayerTable.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.dgPlayerTable.Location = new System.Drawing.Point(108, 12);
      this.dgPlayerTable.Size = new System.Drawing.Size(476, 244);
      this.btnUpdate.Location = new System.Drawing.Point(16, 156);
      this.btnUpdate.Text = "U&pdate";
      this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
      this.btnBind.Location = new System.Drawing.Point(16, 48);
      this.btnBind.Text = "&Bind";
      this.btnBind.Click += new System.EventHandler(this.btnBind_Click);
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(594, 266);
      this.Controls.Add(this.btnBind);
      this.Controls.Add(this.btnUpdate);
      this.Controls.Add(this.dgPlayerTable);
      this.Controls.Add(this.btnLoad);
      this.Controls.Add(this.btnClear);
      this.Controls.Add(this.btnConnect);
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
      this.Name = "Form1";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "OracleDataAdapter Sample";
      this.Load += new System.EventHandler(this.Form1_Load);
      ((System.ComponentModel.ISupportInitialize)(this.dgPlayerTable)).EndInit();
      this.ResumeLayout(false);

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

    private System.Windows.Forms.Button btnConnect;
    private System.Windows.Forms.Button btnClear;
    private System.Windows.Forms.Button btnLoad;
    private System.Windows.Forms.DataGrid dgPlayerTable;
    private System.Windows.Forms.Button btnUpdate;
    private System.Windows.Forms.Button btnBind;

    public OracleConnection oraConn;
    public OracleDataAdapter oraAdapter;
    public OracleCommandBuilder oraBuilder;
    public DataSet dsPlayerTable;

    private void btnConnect_Click(object sender, System.EventArgs e)
    {
      string connString = "User Id=oranetuser; Password=demo; Data Source=oranet";
      if (oraConn.State != ConnectionState.Open)
      {
        try
        {
          oraConn.ConnectionString = connString;

          oraConn.Open();

          MessageBox.Show(oraConn.ConnectionString, "Successful Connection");
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message,"Exception Caught");
        }
      }    
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
      oraConn = new OracleConnection();
    }

    private void btnBind_Click(object sender, System.EventArgs e)
    {
      if (oraConn.State == ConnectionState.Open)
      {
        string strSelect = "select player_num, last_name, first_name, from PlayerTable order by player_num";
        oraAdapter = new OracleDataAdapter(strSelect, oraConn);
        oraBuilder = new OracleCommandBuilder(oraAdapter);
        dsPlayerTable = new DataSet("dsPlayerTable");
        oraAdapter.Fill(dsPlayerTable,"PlayerTable");
        dgPlayerTable.SetDataBinding(dsPlayerTable,"PlayerTable");

        btnBind.Enabled = false;
      }
    }

    private void btnClear_Click(object sender, System.EventArgs e)
    {
      dsPlayerTable.Clear();
      dgPlayerTable.SetDataBinding(null,null);
    }

    private void btnLoad_Click(object sender, System.EventArgs e)
    {
      btnClear_Click(sender, e);

      oraAdapter.Fill(dsPlayerTable,"PlayerTable");

      dgPlayerTable.SetDataBinding(dsPlayerTable,"PlayerTable");      
    }

    private void btnUpdate_Click(object sender, System.EventArgs e)
    {
      oraAdapter.Update(dsPlayerTable,"PlayerTable");
    }
  }








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