C# 如何接收SQL Server事件探查器事件?

C# 如何接收SQL Server事件探查器事件?,c#,sql-server,events,profiling,trace,C#,Sql Server,Events,Profiling,Trace,我是这样受审的: 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 TraceSe

我是这样受审的:

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();
            }
        }
    }
}
错误:

Microsoft.SqlServer.Management.Trace.SqlTraceException:未能 将对象初始化为读取器。-->System.IO.FileLoadException:混合 模式程序集是根据运行时版本“v2.0.50727”和 如果没有其他配置,则无法在4.0运行时中加载 信息

无法将对象初始化为读取器。

这是什么意思


提前感谢

这是什么意思?

System.IO.FileLoadException:混合模式程序集是根据运行时版本“v2.0.50727”构建的,如果没有其他配置信息,无法在4.0运行时中加载。

这意味着您正在以.NET 4进程运行的进程和正在加载的程序集是.NET 2编译的(
Microsoft.SqlServer.Management

重新编译应用程序以使用.NET 2或允许使用.NET 2程序集。

如果针对Microsoft SQL server 2008使用.NETFramework v 4.5在VS 2013中运行代码,则会出现以下错误:

未能将对象初始化为读取器。 混合模式程序集是根据运行时版本“v2.0.50727”生成的,如果没有添加,则无法在4.0运行时中加载 l配置信息

您可以通过更改VS project中的App.config来修复此问题,默认设置为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
但是,可能它也将与110一起工作

我希望,这个解决方案会有所帮助

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup  useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.ConnectionInfoExtended.dll