The functions described in the title are simple, and the example below is also very simple, but you can expand the functions as needed. Here I will just give a reference to them ......, In fact, you have time to create an SQL Server Profiler by yourself.
Step 1:
Add reference: Microsoft. SqlServer. ConnectionInfo Using Microsoft. SqlServer. Management. Trace;
Using Microsoft. SqlServer. Management. Common;
Step 2:
Configure connection information ConnectionInfoBase conninfo = new SqlConnectionInfo ();
(SqlConnectionInfo) conninfo). ServerName = "ServerName ";
(SqlConnectionInfo) conninfo). UserName = "UserName ";
(SqlConnectionInfo) conninfo). Password = "Password ";
(SqlConnectionInfo) conninfo). UseIntegratedSecurity = false;
Step 3:
Create the mytdf. tdf file and put it in the folder where the running program is located.
Note: The. tdf file is used later as a template for reading data. You can create a template in SQL Server Profiler to create a new file.
Read information (including T-SQL statements and so on, add as needed, here only add the ability to get the T-SQL statement being executed) TraceServer trace = new TraceServer ();
Trace. InitializeAsReader (conninfo, "mytdf. tdf ");
While (trace. Read ())
{
// Statements;
Console. WriteLine (trace ["TextData"]);
}
Console. Read ();
Trace. Close ();
All Code: Code
Using System;
Using System. Collections. Generic;
Using System. Text;
Using Microsoft. SqlServer. Management. Trace;
Using Microsoft. SqlServer. Management. Common;
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
ConnectionInfoBase conninfo = new SqlConnectionInfo ();
(SqlConnectionInfo) conninfo). ServerName = "ServerName ";
(SqlConnectionInfo) conninfo). UserName = "UserName ";
(SqlConnectionInfo) conninfo). Password = "Password ";
(SqlConnectionInfo) conninfo). UseIntegratedSecurity = false;
TraceServer trace = new TraceServer ();
Trace. InitializeAsReader (conninfo, "mytdf. tdf ");
While (trace. Read ())
{
// Statements;
Console. WriteLine (trace ["TextData"]);
}
Console. Read ();
Trace. Close ();
}
}
}