System.Data.SqlClient.SqlCommand Class

Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.

See Also: SqlCommand Members

Syntax

[System.ComponentModel.DefaultEvent("RecordsAffected")]
[System.ComponentModel.ToolboxItem("System.Drawing.Design.ToolboxItem, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.Designer("Microsoft.VSDesigner.Data.VS.SqlCommandDesigner, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
public sealed class SqlCommand : System.Data.Common.DbCommand, ICloneable

Remarks

When an instance of System.Data.SqlClient.SqlCommand is created, the read/write properties are set to their initial values. For a list of these values, see the System.Data.SqlClient.SqlCommand constructor.

System.Data.SqlClient.SqlCommand features the following methods for executing commands at a SQL Server database:

SqlCommand.BeginExecuteNonQuery

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this System.Data.SqlClient.SqlCommand, generally executing commands such as INSERT, DELETE, UPDATE, and SET statements. Each call to SqlCommand.BeginExecuteNonQuery must be paired with a call to SqlCommand.EndExecuteNonQuery(IAsyncResult) which finishes the operation, typically on a separate thread.

SqlCommand.BeginExecuteReader

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this System.Data.SqlClient.SqlCommand and retrieves one or more results sets from the server. Each call to SqlCommand.BeginExecuteReader must be paired with a call to SqlCommand.EndExecuteReader(IAsyncResult) which finishes the operation, typically on a separate thread.

SqlCommand.BeginExecuteXmlReader

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this System.Data.SqlClient.SqlCommand. Each call to BeginExecuteXmlReader must be paired with a call to EndExecuteXmlReader, which finishes the operation, typically on a separate thread, and returns an System.Xml.XmlReader object.

SqlCommand.ExecuteReader

Executes commands that return rows. For increased performance, SqlCommand.ExecuteReader invokes commands using the Transact-SQL sp_executesql system stored procedure. Therefore, SqlCommand.ExecuteReader might not have the effect that you want if used to execute commands such as Transact-SQL SET statements.

SqlCommand.ExecuteNonQuery

Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements.

SqlCommand.ExecuteScalar

Retrieves a single value (for example, an aggregate value) from a database.

SqlCommand.ExecuteXmlReader

Sends the SqlCommand.CommandText to the SqlCommand.Connection and builds an System.Xml.XmlReader object.

You can reset the SqlCommand.CommandText property and reuse the System.Data.SqlClient.SqlCommand object. However, you must close the System.Data.SqlClient.SqlDataReader before you can execute a new or previous command.

If a System.Data.SqlClient.SqlException is generated by the method executing a System.Data.SqlClient.SqlCommand, the System.Data.SqlClient.SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server ordinarily closes the System.Data.SqlClient.SqlConnection. However, the user can reopen the connection and continue.

Note:

Nameless, also called ordinal, parameters are not supported by the .NET Framework Data Provider for SQL Server.

Requirements

Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0, 2.0.0.0