2

I have the following scenario;

Dim cmd as New SQLCommand
cmd.Connection = myopenconnection
cmd.CommandText = "usp_getdata"
cmd.Parameters.AddWithValue("@Year", 2008)
cmd.CommandType = StoredProcedure
Dim reader = cmd.ExecuteReader

The application get stuck and keep waiting for a response when the above is excuted. I have tryed to execute the SQL command from SQL Management studio and it works fine and on another copy of the database.

7
  • What does the SP look like? Take a look at this article - perhaps this will explain why you are getting different results from the app than from SSMS. Commented Jan 3, 2012 at 17:53
  • Checked for locks, etc, on the target database? When you run it from management studio, are you sure to use the same login as the .Net applicaiton? Commented Jan 3, 2012 at 17:54
  • Please explain what you do with the reader variable. Commented Jan 3, 2012 at 18:00
  • The reader variable is returned if the .read is sucessful Commented Jan 3, 2012 at 18:04
  • @dems, Yes, I am sure that I am using the same login credentials. Commented Jan 3, 2012 at 18:05

2 Answers 2

2

Execute a stored procedure with no data return

Instead of calling SqlCommand.ExecuteReader(), call SqlCommand.ExecuteNonQuery().

As per this MSDN reference:

ExecuteReader

Executes commands that return rows. For increased performance, ExecuteReader invokes commands using the Transact-SQL sp_executesql system stored procedure. Therefore, ExecuteReader might not have the effect that you want if used to execute commands such as Transact-SQL SET statements.

ExecuteNonQuery

Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements.

EDIT:

Execute a stored procedure to retrieve data

Or if what you're trying to do is return a scalar value, you can use SqlCommand.ExecuteScalar(). But if you're looking to get data, you need to utilize a SqlDataReader object, like so:

Dim queryString As String = "usp_getdata"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.CommandType = CommandType.StoredProcedure
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data.
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using

SqlDataReader Class Reference

Sign up to request clarification or add additional context in comments.

7 Comments

And if it is supposed to return results? usp_getdate implies that ExecuteReader is the right choice.
This doesn't really answer the question though, does it?
@Oded well I would think it does, because if the OP is just calling ExecuteReader() without stepping through it and retrieving values, it would appear as though nothing is happening.
My understanding is that the application hangs on that line, not that the reader is not being utilized. I could be wrong of course.
the SP should return data as it contains a SELECT statement.
|
0

Very slow/not working code;

  Dim cmd As New SQLCommand
  cmd.Connection = myopenconnection
  cmd.CommandText = "usp_getdata"
  cmd.Parameters.AddWithValue("@Year", 2008)
  cmd.CommandType = StoredProcedure
  Dim reader = cmd.ExecuteReader

The problem was solved by modifiying the above code to;

 Dim cmd As New SQLCommand
 cmd.Connection = myopenconnection
 cmd.Parameters.AddWithValue("@Year", 2008)
 cmd.CommandText = "Exec usp_getdata @Year With Recompile"
 cmd.CommandType = Text
 Dim reader = cmd.ExecuteReader

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.