SqlCommand Class
Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.
Assembly: System.Data (in System.Data.dll)
When an instance of SqlCommand is created, the read/write properties are set to their initial values. For a list of these values, see the SqlCommand constructor.
SqlCommand features the following methods for executing commands at a SQL Server database:
Item | Description |
|---|---|
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this SqlCommand, generally executing commands such as INSERT, DELETE, UPDATE, and SET statements. Each call to BeginExecuteNonQuery must be paired with a call to EndExecuteNonQuery which finishes the operation, typically on a separate thread. | |
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this SqlCommand and retrieves one or more results sets from the server. Each call to BeginExecuteReader must be paired with a call to EndExecuteReader which finishes the operation, typically on a separate thread. | |
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this 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 XmlReader object. | |
Executes commands that return rows. For increased performance, ExecuteReader invokes commands using the Transact-SQL sp_executesql system stored procedure. Therefore, ExecuteReader might not have the effect that you want if used to execute commands such as Transact-SQL SET statements. | |
Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements. | |
Retrieves a single value (for example, an aggregate value) from a database. | |
Sends the CommandText to the Connection and builds an XmlReader object. |
You can reset the CommandText property and reuse the SqlCommand object. However, you must close the SqlDataReader before you can execute a new or previous command.
If a SqlException is generated by the method executing a SqlCommand, the SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server ordinarily closes the SqlConnection. However, the user can reopen the connection and continue.
| Topic | Location |
|---|---|
| Walkthrough: Displaying Hierarchical Data in a TreeView Control | Building ASP .NET Web Applications in Visual Studio |
The following example creates a SqlConnection, a SqlCommand, and a SqlDataReader. The example reads through the data, writing it to the console. Finally, the example closes the SqlDataReader and then the SqlConnection as it exits the Using code blocks.
Public Sub ReadOrderData(ByVal connectionString As String) Dim commandText As String = "SELECT OrderID, CustomerID FROM dbo.Orders;" Using connection As New SqlConnection(connectionString) Using command As New SqlCommand(commandText, connection) connection.Open() Using reader As SqlDataReader = command.ExecuteReader() While reader.Read() Console.WriteLine(String.Format("{0}, {1}", _ reader(0), reader(1))) End While End Using End Using End Using End Sub
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.Common.DbCommand
System.Data.SqlClient.SqlCommand
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.