2009-10-13 99 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

检查内部异常会告诉你的配置文件有什么确切的问题,你可能需要做深入的深入研究。