SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );
在我的开发机上它运行得很好,但在Windows 2003服务器上会抛出异常。应用程序通过CGI运行,并具有“完全信任”级别。 我尝试了几个连接字符串,我认为字符串不是引起问题的原因,因为即使是这段代码也会出现异常:
SqlConnection connection = new SqlConnection();
谢谢。
我发现了两个奇怪的事实:
- 如果我连接调试器并简单跟踪代码,它能正常工作。
- 如果我从命令行运行应用程序(而不是通过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()
即使是无参构造函数也会出现异常,因此我认为问题不在连接字符串中。我可以看到堆栈跟踪提示了"路径中有非法字符"的内容。因此,我将尝试深入挖掘并找出原因。但可能已经有人知道解决方案。