DB2 Version 10.1 for Linux, UNIX, and Windows

DB2Command.ExecuteNonQuery Method

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

Namespace:
IBM®.Data.DB2®
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

Syntax

[Visual Basic]
NotOverridable Public Function ExecuteNonQuery() As Integer
[C#]
public int ExecuteNonQuery();
[C++]
public: __sealed int ExecuteNonQuery();
[JScript]
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.

Remarks

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.

Example

[Visual Basic, C#] The following example creates a DB2Command 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)
    myCommand.Connection.Open()
    myCommand.ExecuteNonQuery()
    MyConnection.Close()
End Sub

[C#]
public void CreateMyDB2Command(string myExecuteQuery, string myConnectionString)
{
   DB2Connection myConnection = new DB2Connection(myConnectionString);
   DB2Command myCommand = new DB2Command(myExecuteQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();
}