retrieve SET STATISTICS IO and SET STATISTICS TIME values via ADO.NET?
Asked Answered
P

1

7

When executing T-SQL queries via Management Studio, I can use SET STATISTICS IO ON and SET STATISTICS TIME ON to capture statistics for query tuning.

How can I gather the same statistics info when I'm using .NET client APIs to execute T-SQL queries instead of using Mangaement Studio's UI?

This seems like an obvious thing to be able to do, but after searching MSDN and Google for quite a while, I'm stumped. The closest thing I found was Provider Statistics for SQL Server (ADO.NET) on MSDN, but those stats seem to be client-side stats about network connectivity (e.g. bytes sent/received) from the client's perspective, not the server-side statistics I'm looking for.

Pironi answered 15/2, 2011 at 19:57 Comment(2)
Hi Justin I see your point in the will of getting those info back from a query, would be useful, but how would you imagine to receive those data? When you run ExecuteScalar you get an object back, if you fill a dataset with an adapter, you have a dataset or data table, how can you imagine to get text data back from the server? just a string containing everything what you see in the messages tab of SSMS ?Guenzi
@davide - other than via return values (which you correctly point out won't be much help in cases like ExecuteScalar) the usual way to hook into an operation is via event handlers. Before you call the method you add a handler, and your handler gets called when the query is executed. Take a look at the answer below for more info. I still will have to parse the text coming back, but it's better than having no info at all!Pironi
C
5

See the answer to this question.

The statistics info is just stored in the messages tab.

If you use SET STATISTICS... with this method you should be able to get what you are after.

Caulicle answered 15/2, 2011 at 20:8 Comment(1)
It is important to also read a remark that the OP of the linked question posted: https://mcmap.net/q/549324/-access-to-sql-server-messages-via-ado-net. To wit: one has to call rdr.NextResult() after one has finished with an SqlDataReader. Otherwise it can happen that the event handler gets only output from things like PRINT, SET STATISTICS TIME ON and so forth but not from SET STATISTICS IO ON.Nefertiti

© 2022 - 2024 — McMap. All rights reserved.