共用方式為


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)

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
public final class SqlCeCommand extends DbCommand implements ICloneable
public final class SqlCeCommand extends DbCommand implements ICloneable
Not applicable.

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 Microsoft SQL Server 2005 Compact Edition 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 Edition 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.

Example

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();
}

Inheritance Hierarchy

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

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.

Platforms

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows XP Professional x64 Edition, Windows XP SP2

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0

.NET Compact Framework

Supported in: 2.0, 1.0

See Also

Reference

SqlCeCommand Members
System.Data.SqlServerCe Namespace
SqlCeDataAdapter
SqlCeConnection