SqlCommand
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(stringcmdText
); public SqlCommand(stringcmdText
, SqlConnectionconnection
); public SqlCommand(stringcmdText
, SqlConnectionconnection
, SqlTransactiontransaction
); // Public Instance Properties public string CommandText{set; get; } // implements System.Data.IDbCommand public int CommandTimeout{set; get; } // implements System.Data.IDbCommand public CommandType CommandType{set; get; } // implements System.Data.IDbCommand public SqlConnection Connection{set; get; } public bool DesignTimeVisible{set; get; } public SqlParameterCollection Parameters{get; } public SqlTransaction Transaction{set; get; } public UpdateRowSource UpdatedRowSource{set; get; } // implements System.Data.IDbCommand // Public Instance Methods public void Cancel( ); // implements System.Data.IDbCommand public SqlParameter CreateParameter( ); public int ExecuteNonQuery( ); // implements System.Data.IDbCommand public SqlDataReader ExecuteReader( ); public SqlDataReader ExecuteReader(System.Data.CommandBehaviorbehavior
); public object ExecuteScalar( ); // implements System.Data.IDbCommand public XmlReader ExecuteXmlReader( ); public void Prepare( ); // implements System.Data.IDbCommand public void ResetCommandTimeout( ); }
System.Object
→
System.MarshalByRefObject
→
System.ComponentModel.Component(System.ComponentModel.IComponent
, System.IDisposable)
→
SqlCommand(System.Data.IDbCommand
, System.ICloneable)