What is the difference between ExecuteReader,ExecuteNonQuery
and ExecuteScalar.
Answer Posted / umarali
ExecuteNonQuery():
1.will work with Action Queries only
(Create,Alter,Drop,Insert,Update,Delete).
2.Retruns the count of rows effected by the Query.
3.Return type is int
4.Return value is optional and can be assigned to an integer
variable.
Example-1 for ExecuteNonQuery Method -Insert:
SqlCommand cmd = new SqlCommand("Insert Into SampleTable
Values('1','2')",con);
//con is the connection object
con.Open();
cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executed
Example-2 for ExecuteNonQuery Method - Update:
public void UpdateEmployeeEmail()
{
SqlConnection conn = new SqlConnection(connString))
String sqlQuery = "UPDATE Employee SET
empemail='umar.ali@xyz.com' WHERE empid=5;
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return count;
}
ExecuterReader():
1.will work with Action and Non-Action Queries (Select)
2.Returns the collection of rows selected by the Query.
3.Return type is DataReader.
4.Return value is compulsory and should be assigned to an
another object DataReader.
Example for ExecuteReader Method:
Here, ExecuteReader is used to get set of records by
specified query, namely, "select * from emp"
SqlConnection con = new SqlConnection(constr); //constructor
can be connection of string.
SqlCommand cmd = new SqlCommand ("select * from emp", con);
con.Open();
SqlDataReader dr = cmd. ExecuteReader (CommandBehavior.
CloseConnection); //Implicitly closes the connection because
CommandBehavior. CloseConnection was specified.
while(dr.Read())
{
Console.WriteLine (dr.GetString(0));
}
dr.Close();
ExecuteScalar():
1.will work with Non-Action Queries that contain aggregate
functions.
2.Return the first row and first column value of the query
result.
3.Return type is object.
4.Return value is compulsory and should be assingned to a
variable of required type.
Example-1 for ExecuteScalar Method:
This returns only one value that is first column value of
the first row in the executed query
public int getSomeProdId()
{
int count=0;
SqlConnection conn = new SqlConnection(connString))
String sqlQuery = "SELECT COUNT(*) FROM dbo.region";
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
try
{
conn.Open();
//Since return type is System.Object, a typecast
is must
count = (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return count;
}
Example-2 for ExecuteScalar Method:
This returns one value only, no recordsets.
cmd.CommandText = "Select Name, DOB, from Emp where ID=1";
Dim strName As string = cmd.ExecuteScalar.ToString
Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
can any one find and tell the difference between dot net and php which one is best ? which one we get more salary? which one is stable and which one is best for freshers and also better in future and carrer ? which one we wil get more salary sir ? please send ur valuable suggestions to kiranpulsar2007@gmail.com
What does the hotspot class in .net do?
how to include timer or counting time to display next page in asp.net
What is the appSettings Section in the web.config file?
What are the modes of updation in an updatepanel? What are triggers of an updatepanel?
What is difference between singleton and single call?
What is cross page posting? How is it done?
Define page fragment caching?
How can you implement the postback property of an asp.net control?
What is recordset asp?
Explain the concept of View Model in MVC?
What is synchronous and Asynchronous post back ?
Explain about secure socket layer?
How do I use a proxy server when invoking a web service?
What is the difference between exe and dll?