-->

20 February 2020

ADO.NET OracleCommand

  Asp.Net CS By Example       20 February 2020
 ADO.NET OracleCommand 

 In this post we are exploring more about OracleCommand class. We use an object of the OracleCommand class to represent a SQL Statement or Stored Procedure call that we execute.

  If we have establish a connection with database and we want to make changes in database then Commands Object Comes in picture.The Commands Object used to fetch, add, update, or delete some data, or perhaps modify the database in some other way, usually by running a query.

OracleCommand Properties:

Sr.No Property Type Description
1) CommandText string Gets or sets the SQL statement or stored procedure to execute.
2) CommandTimeout int Gets or sets the number of seconds to wait before ending an attempt to execute the command.
3) CommandType CommandType Gets or sets a value that indicates how the CommandText property is to be interpreted. Typically, the only time we’ll need to set this property is when calling a stored procedure, in which case we set it to CommandType.StoredProcedure.
4) Connection string Gets the name of the database connection.
5) Parameters OracleParameterCollection Gets the parameters (if any) to supply to the command. When using a OracleConnection, the parameters are stored in a OracleParameterCollection object.
6) Transaction OracleTransaction Gets or sets the database transaction for the command.

OracleCommand Methods:

Sr.No Method Return Type Description
1) Cancel() void Cancels the execution of the command.
2) CreateParameter() OracleParameter Creates a new parameter for the command.
3) ExecuteNonQuery() int Used to execute SQL INSERT, UPDATE, and DELETE statements or stored procedures. The int value returned is the number of database rows affected by the command.
4) ExecuteReader() OracleDataReader Also used to execute SQL SELECT statements or stored procedures. Returns the results of the command in a data reader.
5) ExecuteScalar() object Also used to execute SQL SELECT statements or stored procedures that return a single value.Returns the result of the command as an object.
6) Prepare() void Creates a prepared version of the command. This sometimes results in faster execution of the command.
7) ResetCommandTimeout() void Resets the CommandTimeout property to its default value.

 Now here An example showing how to used Command Class.
  Code: Program.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;

namespace CsApp_Connection
{
    class  Program
    {
        public static void Main()
        {
        Console.WriteLine("\tCommand Object Example");
            string connectionString = "data source=LearnAsp;user
                id=LearnAsp;"
            + "password=LearnAsp; Pooling=true; Max pool size=10; Min pool size=5;";

            OracleConnection myConn = new OracleConnection(connectionString);
               Console.WriteLine("\tDB Connection Opening...");
            myConn.Open();
            string selectString = " select * from emp
                ";
            Console.WriteLine("\tCommand Created...");
            OracleCommand myCommand = myConn.CreateCommand();
            myCommand.CommandText = selectString;
            OracleDataAdapter myDataAdapter = new OracleDataAdapter();
            myDataAdapter.SelectCommand = myCommand;
            DataSet myDataSet = new DataSet();

             Console.WriteLine("\tFetching rows from the
                 Emp table");
            string dataTableName = "Emp";
            myDataAdapter.Fill(myDataSet, dataTableName);

            DataTable myDataTable = myDataSet.Tables[dataTableName];

            foreach (DataRow myDataRow in myDataTable.Rows)
            {
                Console.WriteLine("\tEmpno = " + myDataRow["empno"]);
                Console.WriteLine("\tEmp Name = " + myDataRow["ename"]);
                Console.WriteLine("\tJob = " + myDataRow["job"]);
            }

            myConn.Close();
            Console.ReadLine();
        }
    }
} 

Output
Ado.Net Command Object
logoblog

Thanks for reading ADO.NET OracleCommand

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.