This class represents a Transact-SQL command or stored procedure that can be executed against a SQL Server data source. For information about the basic SqlCommand methods and properties, refer to the reference for the System.Data.IDbCommand interface, which SqlCommand implements. SqlCommand errors result in a SqlException being thrown.

The order of parameters isn’t important with the SqlCommand when you create a parameterized query or stored procedure. To create a parameterized query, set CommandType to System.Data.CommandType.Text and name all parameters. For example, you can set the CommandText to:

SELECT * FROM Customers WHERE CustomerID = @CustomerID

to specify that a CustomerID value will be supplied as a parameter with the name @CustomerID .

The SqlCommand class also adds an ExecuteXmlReader( ) method, which executes a SELECT query that uses the FOR XML clause to return the results in an XML document. You can access the nodes of this document in a forward-only, node-by-node basis using the System.Xml.XmlReader instance that ExecuteXmlReader( ) returns. For more information about the FOR XML clause, refer to Chapter 17 or SQL Server 2000 Books Online.

public sealed class SqlCommand : System.ComponentModel.Component, System.Data.IDbCommand, ICloneable {

// Public Constructors

   public SqlCommand( );  

   public SqlCommand(string cmdText);  

   public SqlCommand(string cmdText, SqlConnection connection);

   public SqlCommand(string cmdText, SqlConnection connection, SqlTransaction ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.