Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I receive SQL Server profiler events?

I was tried like so:

using System;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Trace;

namespace SqlProfiller
{
    public partial class Form1 : Form
    {
        TraceServer reader = new TraceServer();
        SqlConnectionInfo connInfo = new SqlConnectionInfo();

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            connInfo.ServerName = @".\SQLR2";
            connInfo.DatabaseName = "DB";
            connInfo.UserName = "sa";
            connInfo.Password = "123";

            reader.InitializeAsReader(connInfo, @"Standard.tdf");


            while (reader.Read())
            {
                Console.WriteLine("SPID  : " + reader["SPID"]);
                Console.WriteLine("Login : " + reader["SessionLoginName"]);
                Console.WriteLine("Object: " + reader["ObjectName"]);
                Console.WriteLine("Text  : " + reader["TextData"]);
                Console.WriteLine();

                textBox1.Text += "Event : " + reader["EventClass"] + Environment.NewLine;
                textBox1.Text += "SPID  : " + reader["SPID"] + Environment.NewLine;
                textBox1.Text += "Login : " + reader["SessionLoginName"] + Environment.NewLine;
                textBox1.Text += "Object: " + reader["ObjectName"] + Environment.NewLine;
                textBox1.Text += "Text  : " + reader["TextData"] + Environment.NewLine;
                textBox1.Text += "----------------------------------------------------------";
                textBox1.Text += Environment.NewLine;
                Application.DoEvents();
            }
        }
    }
}

Error:

Microsoft.SqlServer.Management.Trace.SqlTraceException: Failed to initialize object as reader. ---> System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Failed to initialize object as reader.

What does this mean?

Thanks in advance

like image 428
İsmail Kocacan Avatar asked Feb 20 '23 11:02

İsmail Kocacan


1 Answers

What does this mean?

System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

It means that your process in running as a .NET 4 process and the assembly you are loading is .NET 2 compiled (Microsoft.SqlServer.Management).

Either recompile your application to use .NET 2 or add a hint for .NET 4 in config to allow .NET 2 assemblies.

like image 51
Davin Tryon Avatar answered Feb 22 '23 02:02

Davin Tryon