Interviews31 answers
What is the Return Type of ExecuteNonQuery() method and ExecuteScalar Method?
31 Answers
i--- non
ExecuteScaler ------ object
Mukesh KumarPosted Sep 3, 2017, 12:55 AM EDT
But as I execute command.ExecuteNonQuery();
It returns -1 all the time and even though data has inserted.
what does it means....?
Asif ZaffarPosted Jun 27, 2016, 11:11 PM EDT
ExecuteNonQuery() method: This returns how many rows are affected by the query. Means this is user for basically insert, update , delete query
ExecuteScalar Method: This returns the object. Means the value from a column of table .
Dharmendra RaiPosted Nov 22, 2015, 9:07 PM EST
ExecuteNonQurey will return an integer
ExecuteScala will return an Object.
ExecuteReader will return the DataReader object.
Srinivas PabballaPosted Aug 27, 2015, 5:23 AM EDT
ExecuteNonQuery--> int
ExecutScalar-->Object
Sreekanth ReddyPosted Jul 10, 2015, 10:27 PM EDT
Return Types of Command Methods:-
1- ExecuteNonQuery() - Integer
2-ExecuteScalar() - Object
3-ExecuteReader() - DataReader
Sudheshwer RaiPosted Jun 25, 2015, 9:18 PM EDT
ExecuteNonQuery() returns number of rows affected(ex: 2 rows updated), so return type of ExecuteNonQuery is Integer.
ExecuteScalar() is used to retrieve a single value from database, so return type of ExecuteScalar is Object.
Narasimha Reddy ChennupalliPosted Jun 25, 2015, 10:33 AM EDT
ExecuteNonQuery() : Doesn't return any data but returns affected row count. Return type is integer.
ExecuteScalar Method(): It returns the value only in the first column of the first row. Return type is object.
Sujeet SumanPosted Jun 17, 2015, 7:36 PM EDT
ExecuteNonQuery() - Returns number of values (like table of rows).
ExecuteScaler() - Returns single value.
Jaipal ReddyPosted Jun 17, 2015, 4:18 AM EDT
ExecuteNonQuery() returns : Return a table of rows (lik enumber of rows).
ExecuteScaler() returns : Returns single value or row.
Jaipal ReddyPosted Jun 17, 2015, 4:16 AM EDT
The return type of ExecuteNonQuery() is intezer(int32) type
and ExecuteScalar() returns Object type
Kundan JhaPosted Jun 17, 2015, 1:38 AM EDT
ExecuteNonQuery():-is response to execute a command in connected mode and return integer no. that not fire how many row affected. this method suitable for DML file. (execute return type is integer )
ExecuteScalar()-is also a method which also used to executed method in connected mode but it return a single cells value against the select command that should be first row and first column.(execute return type is object).
bharat aggarwalPosted Jun 3, 2015, 2:20 AM EDT
ExecuteNonQuery() mainly return how many row affected by a DML query i.e. it return an integer value. While ExecuteScalar() method first column of first row value of result set....
Rahul PrajapatPosted May 30, 2015, 5:19 PM EDT
Hello Naitik Jani,
This both method is part of command object and ExecuteNonQuery return type is Int as per as result is concern it will return count of affected row in database.
Talking about ExecuteScalar it will return row or recod from database and return type of ExecuteScalar is Object.
Vaibhav SalwePosted May 30, 2015, 2:33 AM EDT
ExecuteNonQuery is used for basic CRUD operations and returns the no. of rows that have been affected.
Eg. If you try to delete certain rows over a specific data then you will get the no. of rows that have deleted as output.
ExecuteScalar would simply return the value in the First Column of First Row of the output that your query returns.
Vipul MalhotraPosted May 29, 2015, 4:17 AM EDT
ExecuteNonQuery() returns : the number of rows affected
ExecuteScaler() returns : the first column of first row
Alok GuptaPosted May 27, 2015, 12:11 AM EDT
ExecuteScalar() return type is object type.
ExecuteNonQuery() return type is Int32 type
Upendra Pratap ShahiPosted May 26, 2015, 1:19 AM EDT
ExecuteNonQuery returns:- integer
ExecuteScalar returns:- Object
Happy MakwanaPosted May 20, 2015, 3:18 AM EDT
ExecuteNonQuery returns:- integer
ExecuteScalar returns:- Object
Happy MakwanaPosted May 20, 2015, 3:18 AM EDT
Return Value of ExecuteNonQuery is Int32.
Return Value of ExecuteScalar is System.Object.
Manoj BhoirPosted May 16, 2015, 9:46 PM EDT
ExecuteScalar() Method mainly return First Column of First Row in the form of Object
and
ExecuteNonQuery() return now of rows which are affected by DML Query
Pankaj Kumar ChoudharyPosted May 13, 2015, 8:02 PM EDT
ExecuteNonQuery expects to run a command, or a stored procedure, that affects the state of the specified table. This means anything but a query command. You normally use this method to issue an INSERT, UPDATE, DELETE, CREATE, and SET statement.
ExecuteNonQuery returns only the number of rows affected by the command execution, or ?€“1 should this information be unavailable. It doesn’t give you a chance to access any result set generated by the statement or the stored procedure. Actually, there’s really nothing to prevent you from using this method for a query command, but in this case you get neither the resultset nor the number of the affected rows.
cmd.Connection.Open();
nRecsAffected = cmd.ExecuteNonQuery();
cmd.Connection.Close();
// check the record(s) affected here
Posted May 6, 2015, 2:40 AM EDT
integer
Roshan lal YadavPosted May 5, 2015, 9:23 PM EDT
When we manipulate database data in ADO.NET code, ExecuteReader, ExecuteScalar and ExecuteNonQuery are three basic methods extensively used to run queries. These methods are available in SqlCommand, OledbCommand and DbCommand classes under System.Data.SqlClient, System.Data.OleDb and System.Data.Common namespaces respectively. I will explain purpose of these three methods with examples.
ExecuteScalar() Method
ExecuteScalar() method is used to retrieve a single value from database. It executes the defined query and returns the value in the first column of the first row in the selected result set and ignores all other columns and rows in the result set. It is use to get aggregate value from database, for example count or total of rows. So it works with non action queries that use aggregate functions. ExecuteScalar() method is a faster way when we compare it to other ways to retrieve single value from database. It returns a value as object and we have to cast it to appropriate type.
Here is the example of ExecuteScalar() method. Use System.Data.SqlClient namespace for this example.
C#
public string GetCustomerName()
{
string query = "SELECT CustName FROM Customers WHERE CustID = 2";
string connString = "Data Source=local;Initial Catalog=SampleDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
string custName = "";
try
{
conn.Open();
custName = cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
return custName;
}
VB.NET
Public Function GetCustomerName() As String
Dim query As String = "SELECT CustName FROM Customers WHERE CustID = 2"
Dim connString As String = "Data Source=local;Initial Catalog=SampleDB;Integrated Security=True"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand(query, conn)
Dim custName As String = ""
Try
conn.Open()
custName = cmd.ExecuteScalar().ToString()
Catch ex As Exception
Response.Write(ex.Message)
Finally
conn.Close()
End Try
Return custName
End Function
ExecuteScalar() method is used here to retrieve a single value that is customer name for a specific customer ID. It returns object value so we have to cast it in appropriate type “string” to assign it to variable.
ExecuteNonQuery() Method
ExecuteNonQuery() method is used to manipulate data in database and is used for statements without results such as CREATE, INSERT, UPDATE and DELETE commands. It does not return any data but it returns number of rows affected. If NO COUNT property is ON then it will not return number of rows affected. It will not give access to result set generated by the statement. The return value of number of rows affected is of type integer and you can get it in an integer variable. It will tell you how many rows have been affected in result of your statement. ExecuteNonQuery() method is a flexible method and we can use it input and output parameters.
Here is the example of ExecuteNonQuery() method. Use System.Data.SqlClient namespace for this example.
C#
public int DeleteCustomer()
{
string query = "DELETE FROM Customers WHERE CustID = 5";
string connString = "Data Source=local;Initial Catalog=SampleDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
int rowsAffected = 0;
try
{
conn.Open();
rowsAffected = cmd.ExecuteNonQuery(); ;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
return rowsAffected;
}
VB.NET
Public Function DeleteCustomer() As Integer
Dim query As String = "DELETE FROM Customers WHERE CustID = 5"
Dim connString As String = "Data Source=local;Initial Catalog=SampleDB;Integrated Security=True"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand(query, conn)
Dim rowsAffected As Integer = 0
Try
conn.Open()
rowsAffected = cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
conn.Close()
End Try
Return rowsAffected
End Function
ExecuteNonQuery() method is used here to delete a record for specific customer from database table. This method returns number of rows affected as integer.
ExecuteReader() Method
ExecuteReader() method is used with SELECT command. It returns set of rows by executing query or stored procedure mentioned in the command object. When we use ExecuteReader() method, It is necessary that query returns value. It can return one or more result sets as a result of query. The returned data has the DataReader return type. ExecuteReader() method is a read-only and forward-only way of retrieving data. It means we cannot edit data in result set. ExecuteReader() is also a connected way of data retrieval. It uses SELECT statement to read through the table from first to last record in a connected way. Do not use ExecuteReader() method when you know that the result of the query is exactly one record. The better is to use ExecuteScalar() in this situation.
Here is the example of ExecuteReader() method. Use System.Data.SqlClient namespace for this example.
C#
public void GetCustomerDetail()
{
string query = "SELECT * FROM Customers WHERE CustID = 2";
string connString = "Data Source=local;Initial Catalog=SampleDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
string name = "";
string city = "";
string state = "";
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
name = reader["CustName"].ToString();
city = reader["City"].ToString();
state = reader["state"].ToString();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
VB.NET
Public Sub GetCustomerDetail()
Dim query As String = "SELECT * FROM Customers WHERE CustID = 2"
Dim connString As String = "Data Source=local;Initial Catalog=SampleDB;Integrated Security=True"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand(query, conn)
Dim name As String = ""
Dim city As String = ""
Dim state As String = ""
Try
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
name = reader("CustName").ToString()
city = reader("City").ToString()
state = reader("state").ToString()
End While
Catch ex As Exception
Response.Write(ex.Message)
Finally
conn.Close()
End Try
End Sub
ExecuteReader() method is used here to retrieve all detail about a specific customer. It can return any number of records. We have to call the Read() method to read data from result set.
Rajkiran SwainPosted Apr 28, 2015, 7:08 AM EDT
ExecuteNonQuery() returns : the number of rows affected
ExecuteScaler() returns : the first column of first row
Safayat ZisanPosted Apr 23, 2015, 3:15 AM EDT
ReturnType of ExecuteNonQuery is 'int' and ReturnType of ExecuteScalar is object
Shaik Abdul KalamPosted Apr 21, 2015, 4:52 AM EDT
ExecuteNonQuery()'s return type is Int32 and ExecuteSclar's return type is Object.
Nirav VasoyaPosted Apr 21, 2015, 3:34 AM EDT
ExecuteScalar() Method:
ExecuteScalar() method is used to retrieve a single value from database. It executes the defined query and returns the value in the first column of the first row in the selected result set and ignores all other columns and rows in the result set. It is use to get aggregate value from database, for example count or total of rows. So it works with non action queries that use aggregate functions. ExecuteScalar() method is a faster way when we compare it to other ways to retrieve single value from database. It returns a value as object and we have to cast it to appropriate type.
ExecuteNonQuery() Method:
ExecuteNonQuery() method is used to manipulate data in database and is used for statements without results such as CREATE, INSERT, UPDATE and DELETE commands. It does not return any data but it returns number of rows affected. If NO COUNT property is ON then it will not return number of rows affected. It will not give access to result set generated by the statement. The return value of number of rows affected is of type integer and you can get it in an integer variable. It will tell you how many rows have been affected in result of your statement. ExecuteNonQuery() method is a flexible method and we can use it input and output parameters.
Mohan GPosted Apr 19, 2015, 6:50 PM EDT
execute scalar return type is object so must and should we need assign it required type,ExecuteNonquery() is int type so we need assign it to int type variable
Srinivas MPosted Apr 18, 2015, 6:02 AM EDT
ExecuteScalar is typically used when your query returns a single value.
ExecuteNonQuery is typically used for SQL statements without results (e.g., UPDATE, INSERT, etc.). it return integer , either 0 or 1
Munesh SharmaPosted Apr 15, 2015, 10:49 PM EDT
ExecuteNonQuery() returns integer (number of records affected)
ExecuteScalar returns Object
Naitik JaniPosted Apr 14, 2015, 3:27 AM EDT























