Vb.NET ExecuteReader and ExecuteNonQuery


The ExecuteReader method is used to retrieve query results as a DataReader object. The DataReader provides a forward-only, read-only stream of records retrieved from the database. It is optimized for efficient retrieval of data and is particularly useful when you need to sequentially read through a large set of records.

When the ExecuteReader method is invoked, it executes a SELECT command against the database and retrieves the result set. The DataReader allows you to iterate over the records in the result set one by one, starting from the first record and moving forward until the last record is reached.

The DataReader provides methods such as Read(), which advances to the next record in the result set, allowing you to access the data in a sequential manner. Each record can be accessed using the DataReader's various getter methods, such as GetString(), GetInt32(), or GetDateTime(), depending on the data type of the column.

Dim reader As SqlDataReader reader = Command.ExecuteReader() While reader.Read() MsgBox(reader.Item(0)) End While reader.Close()


The ExecuteNonQuery method is used to execute SQL statements that do not return any data. It is commonly used to perform operations such as updating, inserting, or deleting data in a database.

When you invoke the ExecuteNonQuery method, it executes the provided SQL statement against the database and performs the specified operation. This can include updating existing records, inserting new records, deleting records, or any other SQL operation that does not involve retrieving data.

After executing the command, the ExecuteNonQuery method returns the number of rows affected by the operation. This value indicates the number of records that were modified, inserted, or deleted as a result of the SQL statement execution.

Dim retValue As Integer Command = New SqlCommand(Sql, Connection) retValue = Command.ExecuteNonQuery()


The ExecuteNonQuery method is used to execute SQL statements that do not return data, such as update, insert, or delete statements. It returns the number of rows affected by the operation, allowing you to assess the success and impact of the executed command.