asp.net.ph

SqlCommand Class

System.Data.SqlClient Namespace


Represents a Transact-SQL statement or stored procedure to execute at an SQL Server™ database.

SqlCommand Class Members

Collapse   Constructors

Visibility Constructor Parameters
public SqlCommand ( )
public SqlCommand ( String cmdText )
public SqlCommand ( String cmdText , SqlConnection connection )
public SqlCommand ( String cmdText , SqlConnection connection , SqlTransaction transaction )
public SqlCommand ( String cmdText , SqlConnection connection , SqlTransaction transaction , SqlCommandColumnEncryptionSetting columnEncryptionSetting )

Collapse   Properties

Visibility Name Value Type Accessibility
public ColumnEncryptionSetting SqlCommandColumnEncryptionSetting [ Get ]
public CommandText String [ Get , Set ]
public CommandTimeout Int32 [ Get , Set ]
public CommandType CommandType [ Get , Set ]
public Connection SqlConnection [ Get , Set ]
public DesignTimeVisible Boolean [ Get , Set ]
public Notification SqlNotificationRequest [ Get , Set ]
public NotificationAutoEnlist Boolean [ Get , Set ]
public Parameters SqlParameterCollection [ Get ]
public Transaction SqlTransaction [ Get , Set ]
public UpdatedRowSource UpdateRowSource [ Get , Set ]

Collapse   Methods

Visibility Name Parameters Return Type
public BeginExecuteNonQuery ( ) IAsyncResult
public BeginExecuteNonQuery ( AsyncCallback callback , Object stateObject ) IAsyncResult
public BeginExecuteReader ( AsyncCallback callback , Object stateObject ) IAsyncResult
public BeginExecuteReader ( ) IAsyncResult
public BeginExecuteReader ( AsyncCallback callback , Object stateObject , CommandBehavior behavior ) IAsyncResult
public BeginExecuteReader ( CommandBehavior behavior ) IAsyncResult
public BeginExecuteXmlReader ( ) IAsyncResult
public BeginExecuteXmlReader ( AsyncCallback callback , Object stateObject ) IAsyncResult
public Cancel ( ) Void
public Clone ( ) SqlCommand
protected CreateDbParameter ( ) DbParameter
public CreateParameter ( ) SqlParameter
protected Dispose ( Boolean disposing ) Void
public EndExecuteNonQuery ( IAsyncResult asyncResult ) Int32
public EndExecuteReader ( IAsyncResult asyncResult ) SqlDataReader
public EndExecuteXmlReader ( IAsyncResult asyncResult ) XmlReader
protected ExecuteDbDataReader ( CommandBehavior behavior ) DbDataReader
protected ExecuteDbDataReaderAsync ( CommandBehavior behavior , CancellationToken cancellationToken ) Task`1
public ExecuteNonQuery ( ) Int32
public ExecuteNonQueryAsync ( CancellationToken cancellationToken ) Task`1
public ExecuteReader ( ) SqlDataReader
public ExecuteReader ( CommandBehavior behavior ) SqlDataReader
public ExecuteReaderAsync ( CommandBehavior behavior ) Task`1
public ExecuteReaderAsync ( CommandBehavior behavior , CancellationToken cancellationToken ) Task`1
public ExecuteReaderAsync ( CancellationToken cancellationToken ) Task`1
public ExecuteReaderAsync ( ) Task`1
public ExecuteScalar ( ) Object
public ExecuteScalarAsync ( CancellationToken cancellationToken ) Task`1
public ExecuteXmlReader ( ) XmlReader
public ExecuteXmlReaderAsync ( ) Task`1
public ExecuteXmlReaderAsync ( CancellationToken cancellationToken ) Task`1
public Prepare ( ) Void
public ResetCommandTimeout ( ) Void

Collapse   Events

Multicast Name Type
multicast StatementCompleted StatementCompletedEventHandler

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 to execute commands at an 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, DELELE, 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.

If an SqlException is generated by the method executing an 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

The following example initializes an SqlConnection, an SqlCommand, and an SqlDataReader. The example reads through the data, writing it to the console. Finally, the example closes the SqlDataReader, then the SqlConnection.

void ReadMyData ( string connString ) {
   string query = "SELECT OrderID, Customer FROM Orders";
   SqlConnection myConn = new SqlConnection ( connString );
   SqlCommand myCommand = new SqlCommand ( query, myConn );
   myConn.Open ( );
   SqlDataReader myReader = myCommand.ExecuteReader ( );
   try {
      while ( myReader.Read ( ) ) {
         Response.Write ( myReader.GetInt32 ( 0 ) + 
            ", " + myReader.GetString ( 1 ) );
      }
   }
   finally {
      // always close the reader when done.
      myReader.Close ( );
      // always close the connection when done.
      myConn.Close ( );
   }
}
  C# VB

See Also

SqlDataAdapter   SqlConnection Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph