SqlConnection在没有调试的情况下运行时抛出异常 [英] SqlConnection throws an exception when running without debugging

查看:39
本文介绍了SqlConnection在没有调试的情况下运行时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试在不调试的情况下运行我的程序时都会出现异常 (Ctrl+F5).代码在调试时似乎运行良好.我曾尝试使用基本要素创建一个临时项目.我已经重新安装了 Visual Studio.问题仍然存在.已经有一个关于这个的帖子,但没有明确的答案.

I have an exception every time I try to run my program without debugging (Ctrl+F5). The code seems to run fine with debugging. I have tried creating a temp project with just the essentials. I have reinstalled Visual Studio. The problem still occurs. There is already a post about this but not a clear answer.

我的连接字符串:

"Data Source=(localdb)\\ProjectsV12;Initial Catalog=gatdb;Integrated Security=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Trusted_Connection=False;"

异常发生在这里:

(SqlConnection connection = new SqlConnection(connectionString))

这是个例外:

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
  Source=System.Data
  TypeName=System.Data.SqlClient.SqlConnection
  StackTrace:
       at System.Data.SqlClient.SqlConnection..ctor()
       at System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential)
       at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
       at GatHunter.Sql.IsServerConnected(String connectionString) in c:\users\kold\documents\visual studio 2015\Projects\ScatteredGats\GatHunter\Sql.cs:line 14
       at GatHunter.Program.Main(String[] args) in c:\users\kold\documents\visual studio 2015\Projects\ScatteredGats\GatHunter\Program.cs:line 14
  InnerException: 
       HResult=-2146233036
       Message=The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception.
       Source=System.Data
       TypeName=System.Data.SqlClient.SqlConnectionFactory
       StackTrace:
            at System.Data.SqlClient.SqlConnection..cctor()
       InnerException: 
            HResult=-2146233036
            Message=The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception.
            Source=System.Data
            TypeName=System.Data.SqlClient.SqlPerformanceCounters
            StackTrace:
                 at System.Data.SqlClient.SqlConnectionFactory..ctor()
                 at System.Data.SqlClient.SqlConnectionFactory..cctor()
            InnerException: 
                 BareMessage=Configuration system failed to initialize
                 HResult=-2146232062
                 Line=0
                 Message=Configuration system failed to initialize
                 Source=System.Configuration
                 StackTrace:
                      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
                      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
                      at System.Configuration.ConfigurationManager.GetSection(String sectionName)
                      at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
                      at System.Diagnostics.DiagnosticsConfiguration.Initialize()
                      at System.Diagnostics.DiagnosticsConfiguration.get_SwitchSettings()
                      at System.Diagnostics.Switch.InitializeConfigSettings()
                      at System.Diagnostics.Switch.InitializeWithStatus()
                      at System.Diagnostics.Switch.get_SwitchSetting()
                      at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
                      at System.Data.SqlClient.SqlPerformanceCounters..ctor()
                      at System.Data.SqlClient.SqlPerformanceCounters..cctor()
                 InnerException: 
                      HResult=-2146233036
                      Message=The type initializer for 'System.Uri' threw an exception.
                      Source=System.Configuration
                      TypeName=System.Uri
                      StackTrace:
                           at System.Configuration.ClientConfigurationSystem..ctor()
                           at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
                      InnerException: 
                           HResult=-2146233036
                           Message=The type initializer for 'System.UriParser' threw an exception.
                           Source=System
                           TypeName=System.UriParser
                           StackTrace:
                                at System.Uri..cctor()
                           InnerException: 
                                HResult=-2146233036
                                Message=The type initializer for 'System.Runtime.Versioning.BinaryCompatibility' threw an exception.
                                Source=mscorlib
                                TypeName=System.Runtime.Versioning.BinaryCompatibility
                                StackTrace:
                                     at System.Runtime.Versioning.BinaryCompatibility.get_TargetsAtLeast_Desktop_V4_5()
                                     at System.UriParser..cctor()
                                InnerException: 
                                     HResult=-2146233033
                                     Message=Input string was not in a correct format.
                                     Source=mscorlib
                                     StackTrace:
                                          at System.Version.VersionResult.SetFailure(ParseFailureKind failure, String argument)
                                          at System.Version.TryParseComponent(String component, String componentName, VersionResult& result, Int32& parsedComponent)
                                          at System.Version.TryParseVersion(String version, VersionResult& result)
                                          at System.Version.Parse(String input)
                                          at System.Version..ctor(String version)
                                          at System.Runtime.Versioning.BinaryCompatibility.ParseFrameworkName(String frameworkName, String& identifier, Int32& version, String& profile)
                                          at System.Runtime.Versioning.BinaryCompatibility.ParseTargetFrameworkMonikerIntoEnum(String targetFrameworkMoniker, TargetFrameworkId& targetFramework, Int32& targetFrameworkVersion)
                                          at System.Runtime.Versioning.BinaryCompatibility.ReadTargetFrameworkId()
                                          at System.Runtime.Versioning.BinaryCompatibility.get_AppWasBuiltForFramework()
                                          at System.Runtime.Versioning.BinaryCompatibility.BinaryCompatibilityMap..ctor()
                                          at System.Runtime.Versioning.BinaryCompatibility..cctor()
                                     InnerException: 

推荐答案

我要感谢大家的建议.我不知道为什么,但为我解决了这个问题(即使我创建了一个全新的项目并且没有触及 app.config)就是这个.

I want to thank everybody for their advise. I do not know why but what fixed this issue for me (even though I created an entirely new project and didn't touch the app.config) was this.

  1. 删除 app.config
  2. 通过添加->新项目-> app.config重新创建
  3. 将目标框架从 4.6.1 切换到 4.0,然后再切换回 4.6.1

最初我想看看当我切换到 4.0 框架时的反应是什么,令我惊讶的是它突然开始工作了.我知道这听起来很奇怪,我无法解释.

Initially I wanted to see what the reaction was when I switched to the 4.0 framework and to my surprise it suddenly started working. I know this sounds strange and I have no explanation for it.

这篇关于SqlConnection在没有调试的情况下运行时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆