2012-08-16 73 views
4

我被尝试像这样:如何接收SQL Server事件探查器事件?

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: 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.

无法初始化对象作为阅读器。

这是什么意思?

在此先感谢

回答

5

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.

这意味着你在运行一个.NET 4的过程,你正在加载的装配过程编译(Microsoft.SqlServer.Management).NET 2。

重新编译您的应用程序以使用.NET 2或add a hint for .NET 4 in config来允许.NET 2程序集。

3

如果运行的代码(在建2013 VS使用.NETFramework V 4.5针对Microsoft SQL Server 2008中,您收到以下错误:

无法初始化对象作为读者 混合模式组件对内置版本运行时的“V2.0.50727,且不能在4.0运行时加载不additiona L构信息

您可以通过在VS项目改变你的App.config解决这个问题,从默认:

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

这一个(小加成)

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

另外,当我加入大会使用溶液 - >添加装配 - 在下面的路径,而不是“110”>浏览,我使用的“100”的文件夹,:

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

但是,可能它会和110一起工作。

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