Softlion
Softlion

Reputation: 12615

How to detect if MARS is active on the current connection?

i would like to detect if MARS (multiple active results set) is active on the current connection, in order to check for a misconfiguration in a function which needs MARS support.

Either using a TSQL command, or a .NET SQL Server Native Client api method.

Upvotes: 1

Views: 2162

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063864

If checking the connection-string (the best option, IMO) doesn't appeal... perhaps deliberately spoof something that would need MARS, for example open a data reader over two rows, read one of the rows, and (without closing the reader) request another reader-based query.

Note that buffering might impact this, but worth a try...

Upvotes: 1

Peter vd Merwe
Peter vd Merwe

Reputation: 300

You could check the connections ConnectionString property for the presence of the MultipleActiveResultSets attribute and if it is there make sure it is set to true.

Depending on your code usage you can also check the SqlConnection.ServerVersion property to check for MARS support. The major number should be >= 9. (SQL Server 2005 and greater). This would be useful if your code is use on different SQL server versions.

The SqlConnectionStringBuilder could also be used in reverse (maybe a weird idea), it has a MultipleActiveResultSets property.

Upvotes: 3

Related Questions