Command Object Methods - ADO.NET

Q.  What are the Command Object Methods?
- Published on 28 Jul 15

a. ExecuteNonQuery
b. ExecuteReader
c. ExecuteScalar
d. All of the above

ANSWER: All of the above


  • Nihal   -Posted on 19 Oct 15
    Following are the methods of command object.
    ExecuteNonQuery: This method is used,If you are using insert,update,delete SQL statement.Its return type is Integer.This indicates the no of effected records.
    ExecuteReader: This method is used,If you are using Select SQL statement.Its return type is DataReader.
    ExecuteScalar: If you need to return a single value from a sql query, you
    can use the ExecuteScalar method. This method always returns the value
    of the first column from the first row. All other values are ignored. If the result set is empty it will return a null. Its return type is Object.

    ExecuteXMLReader: It returns XML formatted data. Returns a System.Xml.XmlReader
    Example of ExecuteScalar Method
    public void CalculateSalary()
    SqlConnection conn = new SqlConnection();
    SqlCommand cmd;
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;

    cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT SUM(SAL) SAL FROM EMPLOYEE";
    cmd.CommandType = CommandType.Text;

    int TotalSalary = Convert.ToInt32(cmd.ExecuteScalar());

    Label1.Text= "Total Salary is : " + TotalSalary.ToString());

    catch (Exception ex)
    Label1.Text =ex.Message;
    In the above example connection string is configured a for a Data Source in the App.config file

Post your comment / Share knowledge

Enter the code shown above:
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)