This is my (rough) code (DAL):
int i;
// Some other declarations
SqlCommand myCmdObject = new SqlCommand("some query");
conn.open();
i = myCmdObject.ExecuteNonQuery();
conn.close();
The problem is: Even though there is a record present on my SELECT
query, the value in i
remains -1
.
What could be the problem?
Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data. 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.
ExecuteNonQuery() returns number of rows affected(ex: 2 rows updated), so return type of ExecuteNonQuery is Integer.
The ExecuteNonQuery Method returns the number of row(s) affected by either an INSERT , an UPDATE or a DELETE . This method is to be used to perform DML (data manipulation language) statements as stated previously. The ExecuteReader Method will return the result set of a SELECT .
Moreover the executeQuery() is not used in . net but it is used in JAVA. ExecuteNonQuery: Executes Insert, Update, and Delete statements (DML statements) and returns the number of rows affected.
What kind of query do you perform? Using ExecuteNonQuery
is intended for UPDATE
, INSERT
and DELETE
queries. As per the documentation:
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With