Programmatically record all T-SQL statements being executed in SQL SERVER on the current SERVER

Source: Internet
Author: User
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 ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.