Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteNonQuery doesn't return results

Tags:

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?

like image 566
painotpi Avatar asked Mar 18 '11 07:03

painotpi


People also ask

What does command ExecuteNonQuery () return?

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.

Which datatype is returned by using ExecuteNonQuery () method?

ExecuteNonQuery() returns number of rows affected(ex: 2 rows updated), so return type of ExecuteNonQuery is Integer.

Can I use ExecuteNonQuery for select?

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 .

What is the difference between executeQuery and ExecuteNonQuery?

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.


1 Answers

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.

like image 88
Fredrik Mörk Avatar answered Nov 09 '22 18:11

Fredrik Mörk