2009-10-13 153 views
2

尊敬的專業人員請幫我解決以下問題。 在我的.NET C#應用程序中有這樣的代碼:SqlConnection引發異常

SqlConnection connection = new SqlConnection(SQLCONNECTION_STRING); 

它的工作原理奇妙的我的機器上,但拋出的Windows 2003服務器上的異常。該應用程序通過CGI運行並具有「完全信任」級別。 我試過幾個連接串,我認爲該字符串不會導致問題,因爲即使該代碼得到一個例外:

SqlConnection connection = new SqlConnection(); 

感謝。


我發現了兩個奇怪的事實:

  1. 如果我連接調試器來處理和簡單的跟蹤代碼它的工作原理。
  2. 它工作,如果我從命令行運行應用程序(而不是通過CGI根據需要)。

所以我猜CGI和SqlConnection交互有問題。有人知道嗎?

謝謝大家的回覆。


編輯補充:

這裏是我的連接字符串: 「供應商= SQLOLEDB;數據源=(本地);初始目錄=用戶;用戶ID =用戶;密碼=密碼;」

我也試了幾種可能的變體與那裏描述:http://www.connectionstrings.com/sql-server

請找到有關下面的異常信息:

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. 
    at System.Data.SqlClient.SqlConnection..ctor() 
    at Test.Database.UpdateSQLServerDatabase(IDictionary`2 fields, String regName, StringBuilder regCode) 
    at Test.Program.Run() 

Type: System.TypeInitializationException 

InnerException: System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.ArgumentException: Illegal characters in path. 
    at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str) 
    at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) 
    at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path) 
    at System.AppDomainSetup.VerifyDir(String dir, Boolean normalize) 
    at System.AppDomainSetup.get_ConfigurationFile() 
    at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig) 
    at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig) 
    at System.Configuration.ClientConfigurationHost.get_ConfigPaths() 
    at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath) 
    at System.Configuration.ClientConfigurationSystem..ctor() 
    at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() 
    --- End of inner exception stack trace --- 
    at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() 
    at System.Configuration.ConfigurationManager.GetSection(String sectionName) 
    at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName) 
    at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection() 
    at System.Diagnostics.DiagnosticsConfiguration.Initialize() 
    at System.Diagnostics.Switch.InitializeConfigSettings() 
    at System.Diagnostics.Switch.InitializeWithStatus() 
    at System.Diagnostics.Switch.get_SwitchSetting() 
    at System.Diagnostics.TraceSwitch.get_Level() 
    at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp) 
    at System.Data.SqlClient.SqlPerformanceCounters..ctor() 
    at System.Data.SqlClient.SqlPerformanceCounters..cctor() 
    --- End of inner exception stack trace --- 
    at System.Data.SqlClient.SqlConnectionFactory..ctor() 
    at System.Data.SqlClient.SqlConnectionFactory..cctor() 
    --- End of inner exception stack trace --- 
    at System.Data.SqlClient.SqlConnection..cctor() 

即使參數的構造函數給出了一個例外。所以我不認爲問題出在連接字符串中。我可以看到堆棧跟蹤報告了一些關於「路徑中的非法字符」的內容。所以我會試着鑽取並找出答案。但也許有人已經知道解決方案。

+10

拋出什麼樣的異常?例外說什麼? – 2009-10-13 10:55:32

+0

abd你的連接字符串是什麼樣的? – 2009-10-13 11:00:51

回答

0

嘗試以下操作:

string str="Data Source=[YourServer];Initial Catalog=yourdb;User ID=sa;Password=sa;"; 
SqlConnection connection = new SqlConnection(str); 
7

這個問題一般涉及到一個問題,在您的配置文件。您可能想要重新創建它。

感謝, Jivtesh

+0

這個問題對我來說肯定是一個配置問題。我爲所有函數調用設置了模擬(通過serviceAuthorization/impersonateCallerForAllOperations =「true」以及每個方法的OperationBehavior),然後使用不使用模擬的調用進行測試。只是使用WCF測試客戶端或未正確配置的system.serviceModel標記未設置windows/allowedImpersonationLevel =「模擬」將導致此確切錯誤,當您嘗試打開一個SQL連接。 – cl0rkster 2016-02-19 18:31:57

0

對我來說是一絲的web.config配置錯誤,我有一個拼寫錯誤

System.Diagnostincs.TextWriterTraceListener

,而不是

System.Diagnostics。TextWriterTraceListener

並且還有兩個微量元素......修復這兩個問題解決了我的問題。

2

我有這個問題,當我的app.config包括一個空的連接字符串部分,就像這樣;

<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
    </connectionStrings> 
</configuration> 

從它的聲音,你已經有了運行既是一個控制檯應用程序和Web應用程序的一個版本。這意味着您將有一個用於控制檯應用程序的app.config,以及用於asp.net站點的web.config

由於您有一個工作控制檯應用程序,請嘗試找出等效部分之間的差異,並更新web.config以匹配您的app.config

此外,您可能會考慮系統中固有的不同權限 - 網站可能沒有權限連接到數據庫,因爲它的運行方式爲IUSR_<machinename>,但控制檯應用程序正在您自己的用戶憑據下運行,並且你可能是機器管理員。

+0

我試過我贏了窗體和水晶報告它自動添加應用程序配置如果我刪除應用程序配置我的報告不工作,如果我包括我的連接字符串顯示錯誤我添加空字符串如圖所示,但仍然是一個錯誤 – 2015-12-21 10:33:36

+0

這是否有幫助嗎? https://support.microsoft.com/en-us/kb/2872041 – 2015-12-21 10:38:52

0

如果服務是從瀏覽器accesss能,檢查客戶端終結點行爲配置:

<endpointBehaviors> 
     <behavior name="clientEndpoint"> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
      <clientCredentials> 
      <windows allowedImpersonationLevel="Delegation"/> 
      </clientCredentials> 
     </behavior> 
</endpointBehaviors> 
0

SqlConnection connection = new SqlConnection();拋出的錯誤是正確的,但它是由於在App.Config中的錯誤錯誤消息誤導。

0

檢查內部異常會告訴你的配置文件有什麼確切的問題,你可能需要做深入的深入研究。