2012-08-16 67 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一起工作。

我希望這個解決方案會有所幫助。