Export (0) Print
Expand All

SqlCeCommand.ExecuteScalar Method

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.

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

public override Object ExecuteScalar ()
public Object ExecuteScalar ()
public override function ExecuteScalar () : Object

Return Value

The first column of the first row in the result set.

Exception typeCondition

InvalidOperationException

Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

Use the ExecuteScalar method to retrieve a single value (such as an aggregate value) from a data source. This requires less code than using the ExecuteReader method, and then performing the operations necessary to generate the single value by using the data returned by a SqlCeDataReader.

The following example creates a SqlCeCommand and then executes it using ExecuteScalar. The example is passed a string that is an SQL statement, which returns an aggregate result, and a string for connecting to the data source.

SqlCeCommand cmd = new SqlCeCommand("SELECT col1 FROM myTable", conn);
cmd.Connection.Open();
object value = cmd.ExecuteScalar();
conn.Close();

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft