views:

733

answers:

3

I want to create a trace on a database server from my C# app, like what you do in SQL Server Profiler. I have found stored procedures (sys.sp_trace_create etc...) that dont seem to work for my SQL management studio. I was wondering if anyone could help with the coding, or where i would start to do this?!

+3  A: 

Are you wanting this to be real-time, just like the Profiler itself? That'd be tough to do. You'd basically be re-creating the profiler.

If that is not a requirement, I would recommend simply calling the sp_trace_create stored procs that you found to start a server-side trace, then use your application to open up the files that this trace produces.

The EASIEST way to learn how this works is to run the SQL Profiler itself, set all the options you want, then click Run, and immediately click Stop. Then go to File, Export, Script Trace Definition, and choose the appropriate version.

This should give you a TSQL script that puts all the correct pieces together, including all the trace events, columns, etc.

More technical history of the profiler: This old SQLMag article has some history of the Profiler, which replaced an app called "SQL Trace" when SQL 7.0 was introduced.

BradC
A: 

Im not sure about real time, i think i will cross that when i get there. I tried the stored procedures but am getting errors trying to execute them. do you have any ideas how i would call and stop the procedure from my app?

xoxo
A: 

If you're using LINQ to SQL then all of the SQL commands it generates can be sent to the output window (or logged to a file if you want). See here : http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11

GeekyMonkey