What are the methods provided by Command Objects in ADO.NET?

Once when the connection is established, command object is used to define and execute a query.




Command object contains different methods for executing a query. These methods are listed below:

ExecuteNonQuery: When you frame any UPDATE query or DELETE query or INSERT query, you can use ExecuteNonQuery method to execute them. This method will return an integer value indicating how many records in the table have been affected because of the query. Its syntax will be:
int noOfRows = (int)sampleCmd.ExecuteNonQuery();

ExecuteReader: When you frame any SELECT queries, you might want the result set during execution. This result set is returned by ExecuteReader method which actually returns a reader object which when iterated gives the rows and columns of the result. Its syntax is mentioned below:
SqlDataRader sampleReader = sampleCmd.ExecuteReader();

ExecuteScalar: This method can also be used to execute SELECT queries but only a single value will be returned as a result. Even if your query result contains multiple rows and columns, only the first row’s first column will be displayed. If your query is going to return a single value then it is advantageous to use ExecuteScalar method since there is no need for reader object and record iterations. Its syntax is as shown:
string result = (string) sampleCmd.ExecuteScalar();

ExecuteXMLReader: This method is similar to ExecuteReader. But it executes the SELECT query and returns XMLReader object instead of a datareader. This XMLReader object can then be traversed to display the result of the query. Its syntax is shown below:
XmlReader sampleReader = sampleCmd.ExecuteXmlReader();

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How do you implement Observer Design Pattern in .NET? | How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.