SqlCeCommand Class

Represents an SQL statement to execute against a data source.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

public sealed class SqlCeCommand : DbCommand, ICloneable
public final class SqlCeCommand extends DbCommand implements ICloneable
public final class SqlCeCommand extends DbCommand implements ICloneable

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:




Executes commands that return rows.


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


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


Executes commands and returns a result set.

The Data Provider for SQL Server Compact 3.5 SP1 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 3.5 SP1 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.


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

string query = "SELECT [Order ID], [Customer] FROM Orders";
SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand(query, conn);

SqlCeDataReader rdr = cmd.ExecuteReader();

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

    // Always call Close when done reading

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.

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0