Affected rows by ExecuteNonQuery()
Posted by: Miha Svalina
Date: October 17, 2013 05:34AM

I use this code for queries of type insert, update and delete as stated in http://dev.mysql.com/doc/refman/5.5/en/connector-net-tutorials-intro.html:

- ExecuteReader - used to query the database. Results are usually returned in a
MySqlDataReader object, created by ExecuteReader.
- ExecuteNonQuery - used to insert and delete data.
- ExecuteScalar - used to return a single value.

Code:

MySqlCommand cmd = new MySqlCommand(sqlstatement, connection);

for (int i = 0; i < parameters.Length; i++)
  cmd.Parameters.AddWithValue("@" + i.ToString(), parameters);
              
int affectedRows = cmd.ExecuteNonQuery();

lastInsertID = Convert.ToInt32(cmd.LastInsertedId);

I have not found documentation for method ExecuteNonQuery which says that it returns number of affected rows by update, delete or insert query.

Documentation (http://dev.mysql.com/doc/refman/5.5/en/connector-net-ref-mysqlclient.html#connector-net-ref-mysqlclient-mysqlhelper-executenonquery-overloads) for ExecuteNonQuery (overloaded) says only this:

public static int ExecuteNonQuery(MySqlConnectionconnection,
string commandText,
params MySqlParameter[]commandParameters);


Where can I verify that ExecuteNonQuery() returns number of affected rows in documentation. I tested and ExecuteNonQuery() does return number of affected rows, but I would like see description of return value in documentation.

There is RecordsAffected property but it is available by MySQLDataReader class. Is it better to execute insert, update and delete statement using ExecuteReader() method.

Options: ReplyQuote


Subject
Written By
Posted
Affected rows by ExecuteNonQuery()
October 17, 2013 05:34AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.