SqlDataReader Item property loses connection

0

I have this large SQL Server query (legacy) over several tables, that I read with a SqlDataReader. It usually has a limited number of rows to handle, but recently I had to use it over a larger number.

After a number of iterations, the SqlDataReader object, on reading an Item() property, throws a SqlException with the code 0x80131904 and the message "Une erreur de niveau transport s'est produite lors de la réception des résultats du serveur. (provider: TCP Provider, error: 0 - Une connexion existante a dû être fermée par l’hôte distant.)" (More details below.)

The message is in my native French and I'm not entirely sure what the official english version is. My translation would be, after some Web searching, "A transport-level error happened on receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)"

I tried it again: the exception was thrown at the exact same row, reading the exact same field. (Subsequent tries showed the exception might be thrown in different places, but that did not vary wildly : out of eight tries, the exception was thrown in only three different places.

The SqlDataReader object happily reads that same field on the previous Read iterations, and checking the query's results in the database shows that the row where the exception is thrown doesn't have a particular value.

I tried tweaking the query's Where clause to get to the exception faster: it actually read more rows than before, then threw the same exception on a different row. (But again, that was more or less on the same row each time with the tweaked query.)

Looking closer, I put a conditional breakpoint on the instruction that throws the exception. It turns out that:

  • Reading the Item() property with the QuickWatch throws the same exception.
  • Reading the Item() property for another field that hasn't been read yet in the current iteration throws the same exception.
  • Conversely, reading the Item() property for other fields already read in the current iteration throws no exception.

I looked up the SqlDataReader's properties before and after the exception. State changes, going from Open to Closed (which is consistent with the exception). ClientConnectionId changes as well, its new value being an unsurprising series of 0s.

What is happening, and how can I prevent that?

APPENDIX 1

The code that opens the SqlDataReader is as follows:

    Dim z_dreLecteur As IDataReader = Nothing
    Dim z_dcdCommande As IDbCommand = Nothing

[...]

    If TypeOf p_dcxConnexion Is SqlConnection Then
        z_dcdCommande = New SqlCommand(p_strQuery, DirectCast(p_dcxConnexion, SqlConnection))
        Exit Try
    End If

[...]

    z_dcdCommande.CommandTimeout = 600
[...]
    z_dreLecteur = z_dcdCommande.ExecuteReader()
[...]
    z_dcdCommande.Dispose()

p_dcxConnexion is a parameter, already set before the above code runs.

APPENDIX 2 More details on the SqlException. It has an InnerException:

  • type System.ComponentModel.Win32Exception,
  • ErrorCode -2147467259,
  • Message "Une connexion existante a dû être fermée par l’hôte distant" ("An existing connexion was forcibly closed by the remote host"),
  • NativeErrorCode 10054.

The SqlException has:

  • ErrorCode -2146232060,
  • Number 10054,
  • Message "Une erreur de niveau transport s'est produite lors de la réception des résultats du serveur. (provider: TCP Provider, error: 0 - Une connexion existante a dû être fermée par l’hôte distant.)", which probably translates to "A transport-level error happened on receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)"

I can add the StackTrace if needed, but it's really bulky so I won't for now.

vb.net
visual-studio
database-connection
sqldatareader
asked on Stack Overflow Mar 11, 2020 by Jean-David Lanz • edited Mar 12, 2020 by Jean-David Lanz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0