这是简单的例子
String filePath = @"D:\" + FileName;
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText =
@"DECLARE @TraceId INT = (SELECT MAX(id) FROM sys.traces WITH (NOLOCK))
SET @TraceId=@TraceId+1
DECLARE @File NVARCHAR(256);
Set @File= (@filePath)
SET @TraceId=@TraceId+1 --Var olandan bir fazla
DECLARE @MaxFileSize BIGINT = 1 /* max size of file as MegaByte*/
DECLARE @FileCount INT = 1024 /* max file count for write*/
exec sp_trace_create @traceid = @TraceId OUTPUT,
@options = 2,
@tracefile = @File,
@maxfilesize = @MaxFileSize,
@stoptime = NULL,
@filecount = @FileCount
SELECT @TraceId";
command.Parameters.Add(new SqlParameter("@filePath", SqlDbType.NVarChar)).Value = filePath;