在没有调试的情况下运行时,SqlConnection会抛出异常

时间:2015-12-09 04:10:25

标签: c# .net sql-server-2012

每次尝试运行没有调试的程序(Ctrl + F5)时都会出现异常。代码似乎运行良好的调试。我尝试过只创建一个临时项目。我重新安装了Visual Studio。问题仍然存在。关于此问题已经有post但不是一个明确的答案。

我的连接字符串:

"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: 

4 个答案:

答案 0 :(得分:1)

在您的连接字符串中,Integrated Security设置为False

您的连接字符串中缺少

User ID = ****; Password = ****

如果您想使用Windows身份验证连接

Integrated Security=SSPI;代替Integrated Security=true;

如果您尝试将Integrated Security=true/false/yes/no与OleDb提供程序一起使用,则在连接期间会出现异常。

答案 1 :(得分:0)

这主要是由于app.config文件中的错误,检查格式错误的XML或意外元素。

同时仔细检查connectionstring

尝试将连接字符串简化为;

string str="Data Source=(localdb)\\ProjectsV12;Initial Catalog=gatdb;Integrated Security=False;User ID=sa;Password=sa;";
SqlConnection connection = new SqlConnection(str);

再试一次。

答案 2 :(得分:0)

执行以下步骤。

  1. 在桌面上创建“xyz.udl”文件。
  2. 双击该文件。
  3. 选择合适的提供商
  4. 选择服务器
  5. 设置用户/密码并允许保存密码
  6. 选择数据库并点击测试连接。
  7. 点击“确定”按钮。
  8. 在记事本中打开“xyz.udl”文件。
  9. 复制连接字符串并使用它。

答案 3 :(得分:0)

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

  1. 删除app.config
  2. 通过add-> new item->重新创建app.config
  3. 将目标框架从4.6.1切换到4.0然后再切换回4.6.1 试。
  4. 最初我想知道当我切换到4.0框架时的反应是什么,令我惊讶的是它突然开始工作了。我知道这听起来很奇怪,我没有解释。