Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Using OracleCommand to do query

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 btnRetrieve;     private System.Windows.Forms.ListBox listJobs;     public Form1()     {       this.btnConnect = new System.Windows.Forms.Button();       this.btnRetrieve = new System.Windows.Forms.Button();       this.listJobs = new System.Windows.Forms.ListBox();       this.SuspendLayout();       this.btnConnect.Location = new System.Drawing.Point(12, 28);       this.btnConnect.Text = "C&onnect";       this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);       this.btnRetrieve.Location = new System.Drawing.Point(12, 64);       this.btnRetrieve.Text = "&Retrieve";       this.btnRetrieve.Click += new System.EventHandler(this.btnRetrieve_Click);       this.listJobs.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));       this.listJobs.ItemHeight = 15;       this.listJobs.Location = new System.Drawing.Point(100, 28);       this.listJobs.Size = new System.Drawing.Size(524, 244);       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);       this.ClientSize = new System.Drawing.Size(634, 284);       this.Controls.Add(this.listJobs);       this.Controls.Add(this.btnRetrieve);       this.Controls.Add(this.btnConnect);       this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;       this.MaximizeBox = false;       this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;       this.Text = "TableDirect Sample";       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)     {       string connString = "User Id=hr; 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 OracleConnection oraConn;     private void Form1_Load(object sender, System.EventArgs e)     {       oraConn = new OracleConnection();     }     private void btnRetrieve_Click(object sender, System.EventArgs e)     {       OracleCommand cmdEmployees = new OracleCommand();       cmdEmployees.Connection = oraConn;       cmdEmployees.CommandType = CommandType.Text;       cmdEmployees.CommandText = "SELECT * FROM JOBS";       string headText = "Job".PadRight(12);       headText += "Title".PadRight(37);       headText += "Min Salary".PadRight(12);       headText += "Max Salary".PadRight(12);       string headSep = "----";       if (oraConn.State == ConnectionState.Open)       {         try         {           OracleDataReader dataReader = cmdEmployees.ExecuteReader();           listJobs.Items.Add(headText);           listJobs.Items.Add(headSep);           string textLine = "";           while (dataReader.Read())           {             textLine = dataReader.GetString(0);             textLine += dataReader.GetString(1);             textLine += dataReader.GetDecimal(2).ToString();             textLine += dataReader.GetDecimal(3).ToString();             listJobs.Items.Add(textLine);           }         }         catch (Exception ex)         {           MessageBox.Show(ex.Message,"Exception Caught");         }       }       cmdEmployees.Dispose();     }   }