Manoj Kumar Duraisamy
Differentiate ExecuteNonQuery and ExecuteSclar and give the example for both?
By Manoj Kumar Duraisamy 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'

    • 6
  • 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'`

    • 4
  • Bharathi Raja
    Jan, 2018 30

    ExecuteScalar is typically used when your query returns a single value. If it returns more, then the result is the first column of the first row. An example might be SELECT @@IDENTITY AS 'Identity'. ExecuteReader is used for any result set with multiple rows/columns (e.g., SELECT col1, col2 from sometable). ExecuteNonQuery is typically used for SQL statements without results (e.g., UPDATE, INSERT, etc.)

    • 2
  • Mahendra Kumar
    Nov, 2017 23

    ExecuteScalar return object type value but ExecuteNonQuery return int value

    • 2
  • 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)){}

    • 2
  • Gajendra Jangid
    Mar, 2018 12

    ExecuteNonQuery return int... ExecuteSclar return object type

    • 1
  • Sandeep Kumar
    Feb, 2018 28

    ExecuteNonQuery will return all the data from table,but ExecuteSclar will return first row and first column of data only.

    • 1
  • Saravanakumar Sekaran
    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.

    • 1
  • Mukesh Kumar
    Aug, 2017 23

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

    • 1
  • 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

    • 1
  • liaqatk kundi
    Oct, 2021 9

    execute scalar return a single value and execute nonquery return only the number of affected rows.

    • 0
  • Jishan Siddique
    Apr, 2020 30

    Executenonquery is used to Data Manipulation Language(Insert/update/delete) Operation while ExecuteScaler is used to fetch one object only. like Aggrigation function.

    • 0
  • Manoj V
    Sep, 2018 30

    ExecuteNonQuery is effective when you dont want any return values and mainly used for insert, update and delete. Example :- sql = "Insert Into tablename values... your SQL";cnn = new SqlConnection(connetionString);try{cnn.Open();cmd = new SqlCommand(sql, cnn);cmd.ExecuteNonQuery();cmd.Dispose();cnn.Close(); }catch(Exception ex) {ConsoleWriteLine(ex.ToString()); }ExecuteScalar: when you need to return only single value, then it is very effective. Example: sql = "Place the SQL Statement Here like Select Count(*) from GSTTable";cnn = new SqlConnection(connetionString);try{cnn.Open();cmd = new SqlCommand(sql, cnn);Int32 count = Convert.ToInt32(cmd.ExecuteScalar());cmd.Dispose();cnn.Close();MessageBox.Show (" No. of Rows " + count);}catch (Exception ex){ConsoleWriteLine(ex.ToString());}

    • 0
  • Chintan Dave
    Aug, 2018 23

    ExecuteScalar is typically used when your query returns a single value. If it returns more, then the result is the first column of the first row. An example might be SELECT @@IDENTITY AS 'Identity'. ExecuteReader is used for any result set with multiple rows/columns (e.g., SELECT col1, col2 from sometable). ExecuteNonQuery is typically used for SQL statements without results (e.g., UPDATE, INSERT, etc.).

    • 0
  • Jignesh Kumar
    Jul, 2018 28

    ExecuteNonQuery is used to perform DML operation like insert, update and delete. It will return integer values which means how many row affected by this operation.ExecuteSclar returns the value only in the first column of the first row in the result set returned by the SQL statementhttps://www.c-sharpcorner.com/blogs/executescalar-vs-executenonquery1

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS