每次尝试运行没有调试的程序(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:
答案 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)
执行以下步骤。
复制连接字符串并使用它。
答案 3 :(得分:0)
我要感谢大家的建议。我不知道为什么,但是为我解决了这个问题(即使我创建了一个全新的项目并没有触及app.config)就是这样。
最初我想知道当我切换到4.0框架时的反应是什么,令我惊讶的是它突然开始工作了。我知道这听起来很奇怪,我没有解释。