Share via


SqlCeCommand.ExecuteNonQuery Method

Executes an SQL statement against the SqlCeConnection and returns the number of rows affected.

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

Syntax

'Declaration
Public Overrides Function ExecuteNonQuery As Integer
public override int ExecuteNonQuery ()
public:
virtual int ExecuteNonQuery () override
public int ExecuteNonQuery ()
public override function ExecuteNonQuery () : int

Return Value

The number of rows affected.

Exceptions

Exception type Condition
InvalidOperationException

The connection does not exist.

-or-

The connection is not open.

-or-

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

Remarks

You can use ExecuteNonQuery to perform catalog operations. For example, you can use it to query the structure of a database or create database objects, such as tables. You also can use ExecuteNonQuery to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.

Important

ExecuteNonQuery does not use a cursor, and therefore does not place locks on the database. If you are issuing a command that requires a lock, do not use ExecuteNonQuery. Instead, use ExecuteReader.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other DML statements, the return value is -1.

For DDL statements, such as CREATE TABLE or ALTER TABLE, the return value is 0.

Note

This differs from the behavior of System.Data.SqlClient.

Example

The following example creates a SqlCeCommand and then executes it by using ExecuteNonQuery. The example is passed a string that is an SQL statement (such as UPDATE, INSERT, or DELETE) and a string for connecting to the data source.

conn.Open()
Dim cmd As New SqlCeCommand("INSERT INTO foo (col1) VALUES (1)", conn)
cmd.ExecuteNonQuery()
conn.Close()
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO foo (col1) VALUES (1)", conn);
cmd.ExecuteNonQuery();
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.

Platforms

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

See Also

Reference

SqlCeCommand Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace