private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavior run, TdsParserStateObject stateObj)
{
ushort curCmd;
ushort status;
int count;
// Can't retry TryProcessDone
stateObj._syncOverAsync = true;
// status
// command
// rowcount (valid only if DONE_COUNT bit is set)
if (!stateObj.TryReadUInt16(out status))
{
return false;
}
if (!stateObj.TryReadUInt16(out curCmd))
{
return false;
}
long longCount;
if (!stateObj.TryReadInt64(out longCount))
{
return false;
}
count = (int)longCount;
// We get a done token with the attention bit set
if (TdsEnums.DONE_ATTN == (status & TdsEnums.DONE_ATTN))
{
Debug.Assert(TdsEnums.DONE_MORE != (status & TdsEnums.DONE_MORE), "Not expecting DONE_MORE when receiving DONE_ATTN");
Debug.Assert(stateObj._attentionSent, "Received attention done without sending one!");
stateObj._attentionReceived = true;
Debug.Assert(stateObj._inBytesUsed == stateObj._inBytesRead && stateObj._inBytesPacket == 0, "DONE_ATTN received with more data left on wire");
}
if ((null != cmd) && (TdsEnums.DONE_COUNT == (status & TdsEnums.DONE_COUNT)))
{
if (curCmd != TdsEnums.SELECT)
{
cmd.InternalRecordsAffected = count;
}
// Skip the bogus DONE counts sent by the server
if (stateObj._receivedColMetaData || (curCmd != TdsEnums.SELECT))
{
cmd.OnStatementCompleted(count);
}
}
stateObj._receivedColMetaData = false;
// Surface exception for DONE_ERROR in the case we did not receive an error token
// in the stream, but an error occurred. In these cases, we throw a general server error. The
// situations where this can occur are: an invalid buffer received from client, login error
// and the server refused our connection, and the case where we are trying to log in but
// the server has reached its max connection limit. Bottom line, we need to throw general
// error in the cases where we did not receive a error token along with the DONE_ERROR.
if ((TdsEnums.DONE_ERROR == (TdsEnums.DONE_ERROR & status)) && stateObj.ErrorCount == 0 &&
stateObj._errorTokenReceived == false && (RunBehavior.Clean != (RunBehavior.Clean & run)))
{
stateObj.AddError(new SqlError(0, 0, TdsEnums.MIN_ERROR_CLASS, _server, SQLMessage.SevereError(), "", 0));
if (null != reader)
{
if (!reader.IsInitialized)
{
run = RunBehavior.UntilDone;
}
}
}
// Similar to above, only with a more severe error. In this case, if we received
// the done_srverror, this exception will be added to the collection regardless.
// The server will always break the connection in this case.
if ((TdsEnums.DONE_SRVERROR == (TdsEnums.DONE_SRVERROR & status)) && (RunBehavior.Clean != (RunBehavior.Clean & run)))
{
stateObj.AddError(new SqlError(0, 0, TdsEnums.FATAL_ERROR_CLASS, _server, SQLMessage.SevereError(), "", 0));
if (null != reader)
{
if (!reader.IsInitialized)
{
run = RunBehavior.UntilDone;
}
}
}
ProcessSqlStatistics(curCmd, status, count);
// stop if the DONE_MORE bit isn't set (see above for attention handling)
if (TdsEnums.DONE_MORE != (status & TdsEnums.DONE_MORE))
{
stateObj._errorTokenReceived = false;
if (stateObj._inBytesUsed >= stateObj._inBytesRead)
{
stateObj._pendingData = false;
}
}
// _pendingData set by e.g. 'TdsExecuteSQLBatch'
// _hasOpenResult always set to true by 'WriteMarsHeader'
//
if (!stateObj._pendingData && stateObj._hasOpenResult)
{
/*
Debug.Assert(!((sqlTransaction != null && _distributedTransaction != null) ||
(_userStartedLocalTransaction != null && _distributedTransaction != null))
, "ProcessDone - have both distributed and local transactions not null!");
*/
// WebData 112722
stateObj.DecrementOpenResultCount();
}
return true;
}