Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is the difference between abstract class vs interface? Can give me the real time examples?

908


Contrast oop and soa. What are tenets of each16. How does the xmlserializer work? What acl permissions does a process using it require?

935


Can we create a multiple user simultaneously ?

1055


What is the difference between client-side and server-side validations in ASP.NET?

1118


what are the ihttphandler and ihttphandlerfactory interfaces ?

904


What are the namespace classes used in asp.net mvc? : asp.net mvc

990


Explain what is postback in asp. Net?

1022


What is csrf attack in asp.net?

965


what are the security certificates used in webservices?

1948


What I need to create and run an asp.net application?

936


How to use push notification?

990


What is a web based system?

922


How does session state work in asp.net?

971


Explain the difference between dataset and datareader.

952


Do you support digital rights management to protect my videos?

813