当前位置:Gxlcms >
数据库问题 >
用编程的方式将当前服务器上SQL SERVER中正执行的所有T-SQL语句记录下来 <转>
用编程的方式将当前服务器上SQL SERVER中正执行的所有T-SQL语句记录下来 <转>
时间:2021-07-01 10:21:17
帮助过:14人阅读
TraceServer trace = new TraceServer();
trace.InitializeAsReader(conninfo, "mytdf.tdf");
while (trace.Read())
{
//Statements;
Console.WriteLine(trace["TextData"]);
}
Console.Read();
trace.Close();
全部代码:
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();
}
}
}