Notes on returned values of ExecuteNonQuery (): executenonquery
When querying whether data exists in a table, I use ExecuteNonQuery () and judge whether the value is greater than 0 to determine whether the data exists. The results are inconsistent with what I imagined. I found that the result returned after the execution was-1, which I did not understand very well. I checked the information, as shown below: SqlCommand. executeNonQuery method executes a Transact-SQL statement on the connection and returns the number of affected rows. Note: ExecuteNonQuery can be used to perform directory operations (such as querying the database structure or creating database objects such as tables), or by executing UPDATE, INSERT, or DELETE statements, change the data in the database without using DataSet. Although ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are filled with data. For UPDATE, INSERT, and DELETE statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the returned value is-1. The select statement is not suitable for the ExecuteNonQuery () method. It turns out to be so stupid. The method is wrong! I recorded it specially and hoped that my friends would not make similar mistakes!
PublicIntExecuteNonQuery ();
Return Value
Number of affected rows.
Implementation
IDbCommand. ExecuteNonQuery
Remarks
You can useExecuteNonQueryTo perform directory operations (such as querying the database structure or creating database objects such as tables), or by executing UPDATE, INSERT, or DELETE statements, change the data in the database without using DataSet.
AlthoughExecuteNonQueryNo rows are returned, but any output parameters or return values mapped to parameters are filled with data.
For UPDATE, INSERT, and DELETE statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is-1.
[C #] public void CreateMySqlCommand (string myExecuteQuery, SqlConnection myConnection) {SqlCommand myCommand = new SqlCommand (myExecuteQuery, myConnection); myCommand. connection. open (); myCommand. executeNonQuery (); myConnection. close ();}
ExecuteNonQuery () return values
Find the following article.
ExecuteNonQuery () method
I have never cared about this before, and basically didn't use its return value. I checked MSDN, as shown below:
The SqlCommand. ExecuteNonQuery method executes a Transact-SQL statement on the connection and returns the number of affected rows. Note: ExecuteNonQuery can be used to perform directory operations (such as querying the database structure or creating database objects such as tables) or
Update, Insert, or Delete statements. If DataSet is not used, the data in the database is changed. Although ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are filled with data. For Update, Insert, and Delete statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is
-1. If rollback occurs, the returned value is-1.
If the Select clause is executed and the database does not change,-1 is naturally returned. It is also suitable for ODBC and OLE
DB and other data sources, because the IDbCommand. ExecuteNonQuery method is implemented. Function declaration:
[Visual Basic]
Public Overridable Function ExecuteNonQuery () As IntegerImplements_IDbCommand.ExecuteNonQuery implementation:
IDbCommand. ExecuteNonQuery
For IDbCommand. ExecuteNonQuery
Method: Execute SQL statements for the Connection object of the. NET Framework data provider.
Statement, and return the number of affected rows. ExecuteNonQuery can be used to perform directory operations (such as querying the database structure or creating database objects such as tables), or
Update, Insert, or Delete statements. If DataSet is not used, the data in the database is changed. Although ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are filled with data. For all other types of statements, the return value is-1.
What is the return value of commandExecuteNonQuery?
What is the command type?
For OleDbCommand
For UPDATE, INSERT, and DELETE statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the returned value is-1.