Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteNonQuery for SELECT sql statement returning no rows

Tags:

c#

sql-server

how do you check for no rows returned after ExecuteNonQuery for SELECT sql statement returns no rows??

like image 446
Lyle Avatar asked Nov 24 '10 17:11

Lyle


People also ask

What does command ExecuteNonQuery () return?

Execute the command with the ExecuteNonQuery method of the Command object. The ExecuteNonQuery method returns an integer that represents the number of rows affected by the statement or stored procedure that was executed.

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.

What is use of ExecuteNonQuery () method?

ExecuteNonQuery: Use this operation to execute any arbitrary SQL statements in SQL Server if you do not want any result set to be returned. You can use this operation to create database objects or change data in a database by executing UPDATE, INSERT, or DELETE statements.

What is use of ExecuteNonQuery () method Mcq?

ExecuteNonQuery() is used to define number of records affected. you can use it for Insert,Update,Delete functionality. It is use to excute the command which returns no record like our insert,update and delete opration.


4 Answers

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. This method is to be used when you're querying for a bunch of results, such as rows from a table, view, whatever.

The ExecuteScalar Method will return a single value in the first row, first column from a SELECT statement. This method is to be used when you expect only one value from the query to be returned.

In short, that is normal that you have no results from a SELECT statement while using the ExecuteNonQuery method. Use ExecuteReader instead. Using the ExecuteReader method, will will get to know how many rows were returned through the instance of the SqlDataReader object returned.

int rows = 0;

if (reader.HasRows)
    while (reader.Read())
        rows++;

return rows; // Returns the number of rows read from the reader.
like image 147
Will Marcouiller Avatar answered Nov 08 '22 13:11

Will Marcouiller


I don't see any way to do this. Use ExecuteScalar with select count(*) where... to count the rows that match the criteria for your original SELECT query. Example below, paraphrased from here:

using (SqlCommand thisCommand = 
    new SqlCommand("SELECT COUNT(*) FROM Employee", thisConnection))
{
    Console.WriteLine("Number of Employees is: {0}",
       thisCommand.ExecuteScalar());
}

If you need the rows as well, you would already be using ExecuteReader, I imagine.

like image 21
Steve Townsend Avatar answered Nov 08 '22 14:11

Steve Townsend


Use the ExecuteReader method instead. This returns a SqlDataReader, which has a HasRows property.

ExecuteNonQuery shouldn't be used for SELECT statements.

like image 21
mpulcini Avatar answered Nov 08 '22 12:11

mpulcini


This is late, but I ran into this problem recently and thought it would be helpful for others coming in later (like me) seeking help with the same problem. Anyway, I believe you actually could use the ExecuteNonQuery they way you are trying to. BUT... you have to adjust your underlying SELECT query to a stored procedure instead that has SELECT query and an output parameter which is set to equal the row count.

As stated in the MSDN documentation:

Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data.

Given that, here's how I did it. By the way, I would love feedback from the experts out there if there are any flaws in this, but it seems to work for me.

First, your stored procedure should have two SELECT statements: one to return your dataset and another tied to an output parameter to return the record count:

CREATE PROCEDURE spMyStoredProcedure
(
     @TotalRows int output
)
AS
BEGIN
     SELECT * FROM MyTable; //see extra note about this line below.
     SELECT @TotalRows COUNT(*) FROM MyTable;
END

Second, add this code (in vb.net, using SqlCommand etc..).

Dim cn As SqlConnection, cm As SqlCommand, dr As SqlDataReader
Dim myCount As Int32

cn = New SqlConnection("MyConnectionString")
cn.Open() //I open my connection beforehand, but a lot of people open it right before executing the queries. Not sure if it matters.

cm = New SqlCommand("spMyStoredProcedure", cn)
cm.CommandType = CommandType.StoredProcedure
cm.Parameters.Add("@TotalRows", SqlDbType.Int).Direction = ParameterDirection.Output
cm.ExecuteNonQuery()

myCount = CType(cm.Parameters("@TotalRows").Value, Integer)
If myCount > 0 Then
     //Do something.
End If

dr = cm.ExecuteReader()
If dr.HasRows Then
     //Return the actual query results using the stored procedure's 1st SELECT statement
End If
dr.Close()
cn.Close()
dr = Nothing
cm = Nothing
cn = Nothing

That's it.

Extra note. I assumed you may have wanted to get the "MyCount" amount to do something other than determining whether to continue returning you're query. The reason is because with this method, you don't really need to do that. Since I'm utilizing the "ExecuteReader" method after getting the count, I can determine whether to continue returning intended data set using the data reader's "HasRows" property. To return a data set, however, you need a SELECT statement which returns a data set, hence the reason for my 1st SELECT statement in my stored procedure.

By the way, the cool thing about this method of using the "ExecuteNonQuery" method is you can use it to get the total row count before closing the DataReader (you cannot read output parameters before closing the DataReader, which is what I was trying to do, this method gets around that). I'm not sure if there is a performance hit or a flaw in doing this to get around that issue, but like I said... it works for me. =D

like image 40
ptownbro Avatar answered Nov 08 '22 13:11

ptownbro