Sdílet prostřednictvím


SqlCeCommand Class

Represents an SQL statement to execute against a data source.

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Data.Common.DbCommand
        System.Data.SqlServerCe.SqlCeCommand

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

Syntax

'Declaration
Public NotInheritable Class SqlCeCommand _
    Inherits DbCommand _
    Implements ICloneable
'Usage
Dim instance As SqlCeCommand
public sealed class SqlCeCommand : DbCommand, 
    ICloneable
public ref class SqlCeCommand sealed : public DbCommand, 
    ICloneable
[<SealedAttribute>]
type SqlCeCommand =  
    class
        inherit DbCommand
        interface ICloneable
    end
public final class SqlCeCommand extends DbCommand implements ICloneable

The SqlCeCommand type exposes the following members.

Constructors

  Name Description
Public method SqlCeCommand() Initializes a new instance of the SqlCeCommand class.
Public method SqlCeCommand(String) Initializes a new instance of the SqlCeCommand class with the text of the query.
Public method SqlCeCommand(String, SqlCeConnection) Initializes a new instance of the SqlCeCommand class with the text of the query and a SqlCeConnection.
Public method SqlCeCommand(String, SqlCeConnection, SqlCeTransaction) Initializes a new instance of the SqlCeCommand class with the text of the query, a SqlCeConnection, and the SqlCeTransaction.

Top

Properties

  Name Description
Protected property CanRaiseEvents (inherited from Component)
Public property CommandText Gets or sets an SQL statement to execute at the data source. (Overrides DbCommand.CommandText.)
Public property CommandTimeout Gets or sets the wait time before terminating the attempt to execute a command and generating an error. (Overrides DbCommand.CommandTimeout.)
Public property CommandType Gets or sets a value indicating how the CommandText property is interpreted. (Overrides DbCommand.CommandType.)
Public property Connection Gets or sets the SqlCeConnection used by this instance of the SqlCeCommand.
Public property Container (inherited from Component)
Protected property DbConnection (inherited from DbCommand)
Protected property DbParameterCollection (inherited from DbCommand)
Protected property DbTransaction (inherited from DbCommand)
Protected property DesignMode (inherited from Component)
Public property DesignTimeVisible Get always returns false; set always throws a NotSupportedException. (Overrides DbCommand.DesignTimeVisible.)
Protected property Events (inherited from Component)
Public property IndexName Specifies the index to be opened.
Public property Parameters Gets the SqlCeParameterCollection.
Public property Site (inherited from Component)
Public property Transaction Gets or sets the transaction in which the SqlCeCommand executes.
Public property UpdatedRowSource Gets or sets how command results are applied to the DataRow when used by the Update method of the DbDataAdapter. This property should not be used with the .NET Compact Framework Data Provider for SQL Server Compact. (Overrides DbCommand.UpdatedRowSource.)

Top

Methods

  Name Description
Public method Cancel Attempts to cancel the execution of a SqlCeCommand. (Overrides DbCommand.Cancel().)
Protected method CreateDbParameter (inherited from DbCommand)
Public method CreateObjRef (inherited from MarshalByRefObject)
Public method CreateParameter Creates a new instance of a SqlCeParameter object.
Public method Dispose() (inherited from Component)
Protected method Dispose(Boolean) (inherited from Component)
Public method Equals (inherited from Object)
Protected method ExecuteDbDataReader (inherited from DbCommand)
Public method ExecuteNonQuery Executes an SQL statement against the SqlCeConnection and returns the number of rows affected. (Overrides DbCommand.ExecuteNonQuery().)
Public method ExecuteReader() Sends the CommandText to the Connection and builds a SqlCeDataReader.
Public method ExecuteReader(CommandBehavior) Sends the CommandText to the Connection and builds a SqlCeDataReader by using one of the CommandBehavior values.
Public method ExecuteResultSet(ResultSetOptions) Sends the CommandText to the Connection and builds a SqlCeResultSet by using the ResultSetOptions.
Public method ExecuteResultSet(ResultSetOptions, SqlCeResultSet) Sends the CommandText to the Connection and builds a SqlCeResultSet by using the ResultSetOptions.
Public method ExecuteScalar Executes the query and returns the first column of the first row in the result set that is returned by the query. Extra columns or rows are ignored. (Overrides DbCommand.ExecuteScalar().)
Protected method Finalize (inherited from Component)
Public method GetHashCode (inherited from Object)
Public method GetLifetimeService (inherited from MarshalByRefObject)
Protected method GetService (inherited from Component)
Public method GetType (inherited from Object)
Public method InitializeLifetimeService (inherited from MarshalByRefObject)
Protected method MemberwiseClone() (inherited from Object)
Protected method MemberwiseClone(Boolean) (inherited from MarshalByRefObject)
Public method Prepare Creates a prepared (or compiled) version of the command on the data source. (Overrides DbCommand.Prepare().)
Public method SetRange Restricts the set of rows that will be read by the SqlCeDataReader.
Public method ToString (inherited from Component)

Top

Events

  Name Description
Public event Disposed (inherited from Component)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method ICloneable.Clone
Explicit interface implemetationPrivate property IDbCommand.Connection (inherited from DbCommand)
Explicit interface implemetationPrivate method IDbCommand.CreateParameter (inherited from DbCommand)
Explicit interface implemetationPrivate method IDbCommand.ExecuteReader() (inherited from DbCommand)
Explicit interface implemetationPrivate method IDbCommand.ExecuteReader(CommandBehavior) (inherited from DbCommand)
Explicit interface implemetationPrivate property IDbCommand.Parameters (inherited from DbCommand)
Explicit interface implemetationPrivate property IDbCommand.Transaction (inherited from DbCommand)

Top

Remarks

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

SqlCeCommand features the following methods that execute commands at a data source:

Item

Description

ExecuteReader

Executes commands that return rows.

ExecuteNonQuery

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

ExecuteScalar

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

ExecuteResultSet

Executes commands and returns a result set.

The Data Provider for SQL Server Compact does not support batched queries. Commands must be in the following form:

Select * from Customers and not Select * from Customers; Select * from Orders;

If you are using code generated for System.Data.SqlClient, you may have to alter your queries to conform to this restriction.

SQL Server Compact supports multiple simultaneous connections as well as multiple commands sharing the same connection. This means that it is possible to have multiple instances of SqlCeDataReader on the same connection. This behavior differs from that of System.Data.SqlClient.

If a fatal SqlCeException is generated by the method executing a SqlCeCommand, the SqlCeConnection may be closed. You can reopen the connection and continue.

Examples

The following example uses SqlCeCommand, along with SqlCeConnection, to select rows from a database.

Dim query As String = "SELECT [Order ID], [Customer] FROM Orders"
Dim conn As New SqlCeConnection(connString)
Dim cmd As New SqlCeCommand(query, conn)

conn.Open()
Dim rdr As SqlCeDataReader = cmd.ExecuteReader()

Try
    ' Iterate through the results
    '
    While rdr.Read()
        Dim val1 As Integer = rdr.GetInt32(0)
        Dim val2 As String = rdr.GetString(1)
    End While
Finally
    ' Always call Close when done reading
    '
    rdr.Close()

    ' Always call Close when done reading
    '
    conn.Close()
End Try
string query = "SELECT [Order ID], [Customer] FROM Orders";
SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand(query, conn);

conn.Open();
SqlCeDataReader rdr = cmd.ExecuteReader();

try
{
    // Iterate through the results
    //
    while (rdr.Read())
    {
        int val1 = rdr.GetInt32(0);
        string val2 = rdr.GetString(1);
    }
}
finally
{
    // Always call Close when done reading
    //
    rdr.Close();

    // Always call Close when done reading
    //
    conn.Close();
}

Thread Safety

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

See Also

Reference

System.Data.SqlServerCe Namespace

SqlCeDataAdapter

SqlCeConnection