Search code examples
c#odbclong-integerdatareader

Closing ODBCDataReader before finish reading, ODBCData Reader takes long time to close


I'm running a code which calls an SQL query using ODBCDataReader. The result of the query is to be put into an email attachment and mailed to the user. As you know, mail servers have a llimit to how much data can be sent. So what I did was to set a limit to the attachment to a certain size. This causes the ODBCDataReader to finish early before reaching the end of the query

while (_ODBCConn.Data.Read() && AttachmentFileSize < MaxAttachmentSize)
                {
                     //Write to file attachment
                     //Obtain AttahmentFileSize
                }

Unfortunately when I run the next set of code to close the reader, it ends up taking a long time

if (_reader != null)
        {
            _reader.Close();
        }

Does anybody know how to make the reader close faster?


Solution

  • I'm not dodging your question, but I doubt you can do anything about the amount of time it takes for the reader to close. However, if the close command is holding up some time-sensitive code that needs to run afterward you could call that line on a different thread. I would make sure to check the status of the reader before you use it again since the thread can take some time to execute.