SqlConnection引发异常 [英] SqlConnection throws an exception
问题描述
亲爱的专业人员,请为我解决以下问题.在我的.NET C#应用程序中,有以下代码:
Dear professionals please help me with the following problem. In my .NET C# application a have this code:
SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );
它在我的开发计算机上运行非常好,但是在Windows 2003 Server上引发了异常.该应用程序通过CGI运行,并具有完全信任"级别.我尝试了几个连接字符串,但我认为该字符串不会引起问题,因为即使此代码也会出现异常:
It works marvelous on my development machine but throws an exception on Windows 2003 server. The application runs through CGI and has "Full trust" level. I've tried several connections strings and I think the string doesn't cause the issue because even this code gets an exception:
SqlConnection connection = new SqlConnection();
谢谢.
我发现了两个奇怪的事实:
I found two weird facts:
- 如果我将调试器连接到进程并简单地跟踪代码,它将起作用.
- 如果我从命令行(而不是通过CGI按要求运行)运行应用程序,则可以使用.
所以我想CGI和SqlConnection交互有问题.有人知道吗?
So I guess something wrong with CGI and SqlConnection interaction. Does someone know about that?
感谢大家的回应.
编辑后添加:
这是我的连接字符串:提供程序= SQLOLEDB;数据源=(本地);初始目录=用户;用户ID =用户;密码=密码;"
Here is my connection string: "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=Users;User Id=user;Password=password;"
我还尝试了其中所述的几种可能的变体: http://www.connectionstrings.com/sql-server
I've also tried several possible variants as described there: http://www.connectionstrings.com/sql-server
请在以下找到有关异常的信息:
Please find information regarding the exception below:
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()
即使无参数构造函数也会给出异常.因此,我认为问题不在于连接字符串中.我可以看到堆栈跟踪告诉了一些有关路径中的非法字符"的信息.因此,我将尝试深入研究并找出答案.但是也许有人已经知道解决方法.
Even parameterless constructor gives an exception. So I don't think the problem is in the connection string. I can see stack trace tells something about "Illegal characters in path". So I will try to drill down and find out. But maybe someone already knows solution.
推荐答案
此问题通常与您的配置文件中的问题有关.您可能想重新创建它.
This problem is generally related to a problem in your configuration file. You might want to re-create that.
谢谢,吉夫特什
这篇关于SqlConnection引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!