SqlConnection抛出异常

4
亲爱的专业人士,请帮忙解决以下问题。 在我的.NET C#应用程序中,我有以下代码:
SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );

在我的开发机上它运行得很好,但在Windows 2003服务器上会抛出异常。应用程序通过CGI运行,并具有“完全信任”级别。 我尝试了几个连接字符串,我认为字符串不是引起问题的原因,因为即使是这段代码也会出现异常:

SqlConnection connection = new SqlConnection();

谢谢。


我发现了两个奇怪的事实:

  1. 如果我连接调试器并简单跟踪代码,它能正常工作。
  2. 如果我从命令行运行应用程序(而不是通过CGI所需的方式),它也能正常工作。

因此,我猜测CGI和SqlConnection之间有些问题。是否有人知道这方面的情况?

感谢大家的回复。


编辑后添加:

这是我的连接字符串:"Provider=SQLOLEDB;Data Source=(local);Initial Catalog=Users;User Id=user;Password=password;"

我还尝试了描述在这里的几个可能的变体: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
什么样的异常被抛出了?异常信息是什么? - Frederik Gheysels
你的连接字符串是什么样子的? - marc_s
7个回答

9

这个问题通常与您的配置文件有关。您可能需要重新创建它。

谢谢, Jivtesh


对我来说,这个问题肯定是配置问题。我已经设置了所有函数调用的模拟要求(通过serviceAuthorization/impersonateCallerForAllOperations="true"和每个方法上的OperationBehavior),然后测试一个没有使用模拟的调用。仅仅使用WCF测试客户端或未正确配置system.serviceModel标记以设置windows/allowedImpersonationLevel="Impersonation"将在您尝试打开SQL连接时导致此精确错误。 - cl0rkster

2

当我的app.config包含一个空的连接字符串部分时,我遇到了这个问题,就像这样;

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

听起来,你有一个既可以作为控制台应用程序又可以作为Web应用程序运行的版本。这意味着你会有一个用于控制台应用程序的“app.config”,以及一个用于ASP.NET网站的“web.config”。
由于你已经有了一个可工作的控制台应用程序,请尝试查找等效部分之间的差异,并更新“web.config”以与你的“app.config”相匹配。
此外,你可能需要考虑系统中固有的不同权限——Web站点可能没有连接到数据库的权限,因为它是作为“IUSR_”运行的,但控制台应用程序是在你自己的用户凭据下运行的,而你可能是机器管理员。

我尝试了一下,我有一个Win Form和Crystal Report,它自动添加了App Config。如果我删除App Config,我的报表就无法工作。如果我包括我的连接字符串,就会显示错误。我已经按照所示添加了空字符串,但仍然出现错误。 - office 302

0

请尝试以下方法:

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

0
如果可以从浏览器访问服务,请检查客户端端点行为配置:
<endpointBehaviors>
        <behavior name="clientEndpoint">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <clientCredentials>
            <windows allowedImpersonationLevel="Delegation"/>
          </clientCredentials>
        </behavior>
</endpointBehaviors>

0

检查内部异常将告诉您配置文件的确切问题,您可能需要进行深入的钻取。


0

SqlConnection connection = new SqlConnection(); 抛出的错误是正确的,但是由于 App.Config 中的错误消息误导了我们。


0

对我来说,这是web.config跟踪配置不正确,我拼错了

System.Diagnostincs.TextWriterTraceListener

而不是

System.Diagnostics.TextWriterTraceListener

还有两个追踪元素... 修复了这两个问题解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接