This documentation is archived and is not being maintained.

SqlCommand Class

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

For a list of all members of this type, see SqlCommand Members.


[Visual Basic]
NotInheritable Public Class SqlCommand
   Inherits Component
   Implements IDbCommand, ICloneable
public sealed class SqlCommand : Component, IDbCommand, ICloneable
public __gc __sealed class SqlCommand : public Component,
   IDbCommand, ICloneable
public class SqlCommand extends Component implements IDbCommand,

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


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
ExecuteReader Executes commands that return rows. For increased performance, ExecuteReader invokes commands using the Transact-SQL sp_executesql system stored procedure. As a result, ExecuteReader may not have the desired effect if used to execute commands such as Transact-SQL SET statements.
ExecuteNonQuery Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements.
ExecuteScalar Retrieves a single value (for example, an aggregate value) from a database.
ExecuteXmlReader 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 usually closes the SqlConnection. However, the user can reopen the connection and continue.


[Visual Basic, C#, C++] 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, then the SqlConnection.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, Customer FROM Orders"
    Dim myConnection As New SqlConnection(myConnString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    Dim myReader As SqlDataReader = myCommand.ExecuteReader()
        While myReader.Read()
            Console.WriteLine((myReader.GetInt32(0).ToString & ", " & myReader.GetString(1)))
        End While
        ' always call Close when done reading.
        ' always call Close when done reading.
    End Try
End Sub 'ReadMyData

public void ReadMyData(string myConnString) 
   string mySelectQuery = "SELECT OrderID, Customer FROM Orders";
   SqlConnection myConnection = new SqlConnection(myConnString);
   SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
   SqlDataReader myReader = myCommand.ExecuteReader();
     while (myReader.Read()) 
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    // always call Close when done reading.
    // always call Close when done reading.

void ReadMyData(String* myConnString) 
   String* mySelectQuery = S"SELECT OrderID, Customer FROM Orders";
   SqlConnection* myConnection = new SqlConnection(myConnString);
   SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
   SqlDataReader* myReader = myCommand->ExecuteReader();
     while (myReader->Read()) 
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
    // always call Close when done reading.
    // always call Close when done reading.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Data.SqlClient

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

SqlCommand Members | System.Data.SqlClient Namespace | SqlDataAdapter | SqlConnection