This documentation is archived and is not being maintained.

SqlCeCommand.ExecuteNonQuery Method

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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

[Visual Basic]
Public Overridable Function ExecuteNonQuery() As Integer Implements _
public virtual int ExecuteNonQuery();
public: virtual int ExecuteNonQuery();
public function ExecuteNonQuery() : int;

Return Value

The number of rows affected.




Exception Type Condition
InvalidOperationException The connection does not exist.


The connection is not open.

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


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.

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.


[Visual Basic, C#] 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.

[Visual Basic] 
Public Sub CreateMySqlCeCommand(myExecuteQuery As String, myConnection As SqlCeConnection)
    Dim myCommand As New SqlCeCommand(myExecuteQuery, myConnection)
End Sub 

public void CreateMySqlCeCommand(string myExecuteQuery, SqlCeConnection myConnection) {
    SqlCeCommand myCommand = new SqlCeCommand(myExecuteQuery, myConnection);

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: .NET Compact Framework

.NET Framework Security: 

See Also

SqlCeCommand Class | SqlCeCommand Members | System.Data.SqlServerCe Namespace