Export (0) Print
Expand All

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.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.SqlClient.SqlCommand

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

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.

Remarks

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.

Example

[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)
    myConnection.Open()
    Dim myReader As SqlDataReader = myCommand.ExecuteReader()
    Try
        While myReader.Read()
            Console.WriteLine((myReader.GetInt32(0).ToString & ", " & myReader.GetString(1)))
        End While
    Finally
        ' always call Close when done reading.
        myReader.Close()
        ' always call Close when done reading.
        myConnection.Close()
    End Try
End Sub 'ReadMyData

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

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

[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.

Requirements

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

Show:
© 2014 Microsoft