Google News
logo
ADO.NET - Interview Questions
What you understand by ExecuteNonQuery Method?
The ExecuteNonQuery method is used to execute the command and return the number of rows affected.
 
The ExecuteNonQuery method cannot be used to return the result set.
 
Snippets working with ExecuteNonQuery
public void CallExecuteNonQuery()
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
    try
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "DELETE FROM EMP WHERE DEPTNO = 40";
        cmd.CommandType = CommandType.Text;
        conn.Open();
        Int32 RowsAffected = cmd.ExecuteNonQuery();
        MessageBox.Show(RowsAffected + " rows affected", "Message");
        cmd.Dispose();
        conn.Dispose();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Advertisement