Using Oracle Connection String In Windows Form

Today, I need to make aN SMS form to get data form local server and send messages to clients using a mobile company API. I decided to share my knowledge.

Let's start!

Step 1

I order to make a working form with oracle database we need the following three classes.
  1. OracleConnection
  2. OracleCommand
  3. OracleDataReader

I just created three objects for all the three classes: con for OracleConnection or cmd for OracleCommand and datareader for OracleDataReader. I initialized first two classes using new but did not initialized the third one as you can see in the attached code.

Now you may see a red squiggling line below the classes it means you did not included the reference of System.Data.OracleClient.dll. In my case I found it here:


Step 2

Now you need to tell object con about the connection string and the connection. The following code shows when the form will open, it will connect with database and then will open the connection.

  1. public Form1()  
  2. {   
  3.    InitializeComponent();  
  4.    con.ConnectionString = "User Id=id;password=db_pass;Data Source=db_name;";  
  5.    con.Open();   
  6. }  
Step 3

And now you are good to go with database commands, I have written a separate function for this:
  1. public void OracleQuery()  
  2. {   
  3.    string sql = "select * from MESSSAGES where STATUS='Pending' order by ID ASC";  
  4.    orcmd.Connection = con;  
  5.    orcmd.CommandText = sql;  
  6.    getListItems();  
  7. }  
As you can see we are providing knowledge to orcmd about the connection and then the command to work after this there is a function and its definition is here,
  1. private void getListItems()  
  2. {  
  3.     datareader = orcmd.ExecuteReader();  
  4.     string acnt_status = "Pending";  
  5.     while(datareader.Read())  
  6.     {  
  7.         var status = datareader[4];  
  8.         if(status.Equals(acnt_status))  
  9.         {  
  10.             var contact_no = datareader[1];  
  11.             var contactLength = contact_no.ToString()  
  12.                 .Length;  
  13.             if(contactLength < 12)  
  14.             {  
  15.                 listView1.LabelWrap = false;  
  16.                 listView1.Items.Add("The Contact Number is wrong " + contact_no);  
  17.                 break;  
  18.             }  
  19.             listBox1.Items.Add(datareader[0] + " " + contact_no + " " + status + " " + contactLength);  
  20.         }  
  21.         else if(!status.Equals(acnt_status))  
  22.         {  
  23.             MessageBox.Show("No pending field found");  
  24.             break;  
  25.         }  
  26.     }  
  27.     return;  
  28. }  
In the function above as you can see datareader initialized with orcmd.ExecuteReader(), so simple as the name tells its definitions, we are going to read columns from the database. Columns in the database are indexed from Zero (0). If you want to go to first column just write datareader[0] and access the ID column. All the data from the database is shown in the list box. And there is an if that if there is any number less than 12 it will thrown out in listview.

Step 4

I explained my code and now you can make necessary changes as per your requirements.

Learn code and share it.

Your suggestions and comments will be highly appreciated and helpful to me and the readers.

Up Next
    Ebook Download
    View all
    View all