Manoj Kumar
Differentiate ExecuteNonQuery and ExecuteSclar and give the example for both?
By Manoj Kumar in ADO.NET on Dec 27 2016
  • Srikanth Gogineni
    May, 2017 5

    ExecuteScalar will return a single value ex: select name from emp where empid='1'ExecuteNonQuery will return the count of effected rows and it will be applicable for dml commands delete from emp where empid='1'

    • 3
  • Mahendra Kumar
    Nov, 2017 23

    ExecuteScalar return object type value but ExecuteNonQuery return int value

    • 1
  • Subhashkumar Yadav
    Feb, 2017 23

    - **ExecuteScalar** is going to be the typeof query which will be returning asingle value. *An example would be returning a generated id after inserting.*`INSERT INTO my_profile (Address) VALUES ('123 Fake St.');SELECT CAST(scope_identity() AS int)`- **ExecuteReader** gives you a data readerback which will allow you to read allof the columns of the results a rowat a time. *An example would be pulling profile information for one or more users.*`SELECT * FROM my_profile WHERE id = '123456'`- **ExecuteNonQuery** is any SQL which isn't returning values, but isactually performing some form of worklike inserting deleting or modifyingsomething. *An example would be updating a user's profile in the database.*`UPDATE my_profile SET Address = '123 Fake St.' WHERE id = '123456'`

    • 1
  • S.Saravana Kumar
    Jan, 2018 11

    execute scalar return a single value and execute nonquery does not return data at all: only the number of rows affected by an insert, update, or delete.

    • 0
  • Mahalakshmi Meganathan
    Nov, 2017 6

    ExcuteNonQuery is used for Except Retrieve(Select) the data from DB EX:using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TMSConnectionString"].ConnectionString)){con.Open();string msg = string.Empty;string lstr = "Insert Into UserInfo1 (Name) VALUES(@Name)";SqlCommand cmd = new SqlCommand(lstr, con);cmd.Parameters.AddWithValue("@Name", txtname.Text);int recordcount = cmd.ExecuteNonQuery();if (recordcount>0){msg = "Record Saved sucessfully";} }ExecuteScalar:It will retrieve the first row and column get from DB using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TMSConnectionString"].ConnectionString)){con.Open();string msg = string.Empty;string lstr = "Select Name from UserInfo1";SqlCommand cmd = new SqlCommand(lstr, con);string result = (string)cmd.ExecuteScalar();if (!string.IsNullOrEmpty(result)){}

    • 0
  • Mukesh Kumar
    Sep, 2017 20

    Scaler returns single object

    • 0
  • Mukesh Kumar
    Aug, 2017 28

    ExecuteNonQuery is used to select more data while ExecuteScaler is used to select only one object

    • 0
  • Mukesh Kumar
    Aug, 2017 23

    Executenonquery is used to insert update &delete while ExecuteScaler is used to fetch one object only

    • 0
  • NIRANJAN PILANIA
    Aug, 2017 8

    ExecuteNonQuery working with Action Query like Insert,Delete,UpdateExecuteSclar working with NonAction Query plus Agrigate Function like Select Conut(*),Select Max,Select Min

    • 0