Simulating SQL Server Profiler in a C # Application?

I want to create a trace on a database server from my C # application, for example, what are you doing in SQL Server Profiler. I found stored procedures (sys.sp_trace_create, etc.) that don't seem to work for my SQL management studio. I was wondering if anyone can help with coding or where will I start to do this?

+3
source share
3 answers

Do you want it to be in real time, just like a profiler? That would be hard to do. You will mostly recreate the profiler.

If this is not a requirement, I would recommend just calling the sp_trace_create stored procedures that you found to run the server-side trace, and then use the application to open the files created by this trace.

The easiest way to find out how this works is to start the SQL profiler, set all the necessary parameters, then click Run and immediately click Stop. Then go to File, Export, Script Definition of the trace and select the appropriate version.

This should give you a TSQL Script that combines all the correct fragments, including all trace events, columns, etc.

: SQLMag Profiler, "SQL Trace", SQL 7.0.

+3

,

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();        
}
+1

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

0
source

All Articles