DB2Command.ExecuteNonQuery Method

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

IBM.Data.DB2 (in IBM.Data.DB2.dll)


[Visual Basic]
NotOverridable Public Function ExecuteNonQuery() As Integer
public int ExecuteNonQuery();
public: __sealed int ExecuteNonQuery();
public function ExecuteNonQuery() : int;

Return value

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


You can use ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables); or to change the data in a database, without using a DataSet, by executing UPDATE, INSERT, or DELETE statements.

You can also use ExecuteNonQuery to execute multiple SQL statements. In this case, the return value is the number of rows affected by all statements in the command.

Although ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.


[Visual Basic, C#] The following example creates a DB2®Command 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 to use to connect to the database.

[Visual Basic]
Public Sub CreateMyDB2Command(myExecuteQuery As String, _
myConnectionString As String)
    Dim myConnection As New DB2Connection(myConnectionString)
    Dim myCommand As New DB2Command(myExecuteQuery, myConnection)
End Sub

public void CreateMyDB2Command(string myExecuteQuery, string myConnectionString)
   DB2Connection myConnection = new DB2Connection(myConnectionString);
   DB2Command myCommand = new DB2Command(myExecuteQuery, myConnection);