İsmail Kocacan
İsmail Kocacan

Reputation: 1234

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

Upvotes: 4

Views: 2765

Answers (2)

Alex Zarutin
Alex Zarutin

Reputation: 31

If running your code (build in VS 2013 using .NETFramework v 4.5 against Microsoft SQL server 2008, you are getting the following error:

Failed to initialize object as reader. Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additiona l configuration information.

You can fix it by changing your App.config in VS project, from default:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

to this one (small addition)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup  useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

Plus, when I added Assembly using Solution -> add Assembly -> Browse, I used "100" folder, instead of "110" in the path below:

C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.ConnectionInfoExtended.dll

But, may be it will be working with 110 as well.

I hope, this solution will help.

Upvotes: 3

Davin Tryon
Davin Tryon

Reputation: 67296

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.

Upvotes: 5

Related Questions