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'
- **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'`
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.)
ExecuteScalar return object type value but ExecuteNonQuery return int value
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)){}
ExecuteNonQuery return int... ExecuteSclar return object type
ExecuteNonQuery will return all the data from table,but ExecuteSclar will return first row and first column of data only.
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.
Executenonquery is used to insert update &delete while ExecuteScaler is used to fetch one object only
ExecuteNonQuery working with Action Query like Insert,Delete,UpdateExecuteSclar working with NonAction Query plus Agrigate Function like Select Conut(*),Select Max,Select Min
execute scalar return a single value and execute nonquery return only the number of affected rows.
Executenonquery is used to Data Manipulation Language(Insert/update/delete) Operation while ExecuteScaler is used to fetch one object only. like Aggrigation function.
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());}
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.).
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