Question

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?!

Was it helpful?

Solution

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.

OTHER TIPS

If you are still interested, found this code

public void FileToTable()
{
    TraceServer reader = new TraceServer();

    ConnectionInfoBase ci = new SqlConnectionInfo("localhost");
    ((SqlConnectionInfo)ci).UseIntegratedSecurity = true;

    reader.InitializeAsReader(ci, @"Standard.tdf");

    int eventNumber = 0;

    while (reader.Read())
    {
        Console.Write( "{0}\n", reader.GetValue(0).ToString() );
    }
    reader.Close();        
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top