Compartilhar 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
'Usage
Dim instance As SqlCeCommand
Dim returnValue As Integer

returnValue = instance.ExecuteNonQuery
public override int ExecuteNonQuery ()
public:
virtual int ExecuteNonQuery () override
public int ExecuteNonQuery ()
public override function ExecuteNonQuery () : int
Not applicable.

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

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 Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace