Results 1 to 4 of 4

Thread: SqlCommand.ExecuteReader takes ages to return

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    SqlCommand.ExecuteReader takes ages to return

    I have the following block of code in my program:

    Code:
                '' create a command object 
                Dim cmd As New SqlCommand
                cmd.CommandText = "SomeStoredProc"
                cmd.CommandType = CommandType.StoredProcedure
                cmd.CommandTimeout = 0
                cmd.Parameters.AddRange(SpParmList.ToArray)     ' SpParmList is a List of parameters
    
                '' Get records from database
                Using cnn As New SqlClient.SqlConnection(DbConnectionString)
                    cnn.Open()
                    cmd.Connection = cnn
                    Using reader As SqlDataReader = cmd.ExecuteReader()      '<-- this line
                        myDS.Load(reader)
                    End Using
                End Using
    The problem is that it gets stuck at the highlighted lines somethimes, but not always.
    Sometimes it takes just a few seconds on that line, while at other times it takes even more than half an hour, for the same stored procedure.

    Note: The stored procedure returns millions of records.

    Anyone knows why this happens and what can be done for this?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SqlCommand.ExecuteReader takes ages to return

    Run SQL Profiler and start working to optimize the query... look at what's also going on, if there are locks, scans or other things in the way. Check to make sure you have indexes in all the right places and that they are up to date. Are you doing inserts or updates? or is it a select only?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SqlCommand.ExecuteReader takes ages to return

    Quote Originally Posted by techgnome View Post
    Run SQL Profiler and start working to optimize the query... look at what's also going on, if there are locks, scans or other things in the way. Check to make sure you have indexes in all the right places and that they are up to date. Are you doing inserts or updates? or is it a select only?

    -tg
    I would prefer to leave that part to the developers of the Stored Procedures.

    Actually this code is part of a common method in the DAL layer, and is used to execute Stored Procedures. The "myDS" is an instance of a class I developed to hold data that comes from the Reader, since the .NET DataTables can't hold this much amount of data and starts giving SystemOutOfMemory errors. The myDS.Read method accepts an IDataReader object and gets the data out of it into its collection by calling the DataReader.Read method in a loop (just the way you would read from a DataReader, nothing different).

    The problem is that the program gets stuck at a line before the Read/Load method is called, and so has nothing to do with my class. This time taken is erratic and can range from a few seconds to more than half an hour, for the same stored procedure. I would guess that there is some sort of caching going on at the SQL Server end, but not sure.

    I had set the CommandTimeout = 0 so that atleast the command doesn't time out. Is there anything else that can be done? Also I want to know whether the stored procedure is executed on the sql server as soon as the .ExecuteReader() method is called, or when we start reading the data out of it?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SqlCommand.ExecuteReader takes ages to return

    I tried running the stored procedures causing problems from SSMS several times. They seem to be running fine and usually give the result in maximum one to two minutes.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width