Monday, 28 April 2014

Database connectivity in C# .NET


  • First you have to install oracle database and oracle client. I'm using Oracle Database 10g Express Edition and Oracle Client 10g Express Edition.
  • Open Visual Studio. Make a project (ASP.net or win forms or console). I'm doing it for console.

  • Copy and paste the code for Connect function.
  • You will see the errors.
  • For removing these errors.
    • Click View->Solution explorer.
    • In Solution Explorer, Right click :- "References"
    • Click "Add Reference"
    • Click "Browse"
    • Go to the path : (Oracle client installation directory.)\bin
      • In my case it is: "C:\XEClient\bin"
    • Select Oracle.DataAcess.dll
    • Click Add->Ok
  • Here is a code for .NET connectivity with an Oracle database
static void Connect()
using (var myConnection = new OracleConnection("Data Source=QUQNUS-PC;Persist Security Info=True;User ID=ce30;Password=yup;")) 
{
                //Open the Connection object
                myConnection.Open();
                //creating a command object
                OracleCommand comm = myConnection.CreateCommand();
                //writing query for retreival
                comm.CommandText = "Select * FROM HALLO";
                //Execute the query
                OracleDataReader s = comm.ExecuteReader();
                while (s.Read())
                {
                    Console.WriteLine(s.GetValue(0) + " " + s.GetValue(1));
                }
                //writing query for insertion
                comm.CommandText = "INSERT INTO HALLO values('maba','2011-ce-76')";
                //execute inserion
                int k = comm.ExecuteNonQuery();
                Console.WriteLine("--------Now after Insertion--------");
                //writing query for retreival for checking insertion
                comm.CommandText = "Select * FROM HALLO";
                //Execute the query
                s = comm.ExecuteReader();
                while (s.Read())
                {
                    Console.WriteLine(s.GetValue(0) + " " + s.GetValue(1));
                }
            }
        }

I hope this update will help you.

No comments:

Post a Comment

Contributors