简体   繁体   English

创建对象时出现SQL连接异常问题

[英]Problem with SQL Connection exception on creating object

I created a SQL Server database in Visual Studio 2013. 我在Visual Studio 2013中创建了一个SQL Server数据库。

I have a problem when I am trying to create a new SqlConnection object. 尝试创建新的SqlConnection对象时遇到问题。 The same code worked yesterday, but now I have some problem. 相同的代码昨天工作了,但是现在我遇到了一些问题。 I also added the SQLException but I can't reach this code 我还添加了SQLException但无法访问此代码

try
{
    string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

    SqlConnection sqlConnection = new SqlConnection(connectionString);
    sqlConnection.Open();
}
catch (System.Data.SqlClient.SqlException sqlException)
{
    System.Windows.Forms.MessageBox.Show(sqlException.Message);
}

Application.Run(new LoginForm());

SqlConnection did not work. SqlConnection不起作用。 This is the exception. 这是例外。 I don't know what it is wrong because it did work before 我不知道这是怎么回事,因为它以前确实起作用

System.TypeInitializationException was unhandled System.TypeInitializationException未处理
HResult=-2146233036 HResult = -2146233036
Message=The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. Message ='System.Data.SqlClient.SqlConnection'的类型初始值设定项引发了异常。
Source=System.Data 源= System.Data
TypeName=System.Data.SqlClient.SqlConnection TypeName = System.Data.SqlClient.SqlConnection

StackTrace: 堆栈跟踪:

at System.Data.SqlClient.SqlConnection..ctor(String connectionString) 在System.Data.SqlClient.SqlConnection..ctor(String connectionString)

InnerException: System.InvalidOperationException InnerException:System.InvalidOperationException
HResult=-2146233079 HResult = -2146233079
Message=Failed to read the configuration section for enclave providers. Message =无法读取安全区域提供程序的配置部分。 Make sure the section is correctly formatted in your application configuration file. 确保在应用程序配置文件中该部分的格式正确。

Error Message: Configuration system failed to initialize 错误消息:配置系统无法初始化
Source=System.Data 源= System.Data

StackTrace: 堆栈跟踪:

at System.Data.SqlClient.SqlConnection..cctor() 在System.Data.SqlClient.SqlConnection..cctor()

InnerException: System.Configuration.ConfigurationErrorsException InnerException:System.Configuration.ConfigurationErrorsException
HResult=-2146232062 HResult = -2146232062
Message=Configuration system failed to initialize 消息=配置系统无法初始化
Source=System.Configuration 源= System.Configuration
BareMessage=Configuration system failed to initialize BareMessage =配置系统无法初始化
Line=0 行= 0

InnerException: System.Configuration.ConfigurationErrorsException InnerException:System.Configuration.ConfigurationErrorsException
HResult=-2146232062 HResult = -2146232062
Message=Root element is missing. Message =根元素丢失。 Source=System.Configuration 源= System.Configuration
BareMessage=Root element is missing. BareMessage =根元素丢失。

Line=0 行= 0

InnerException: System.Xml.XmlException InnerException:System.Xml.XmlException
HResult=-2146232000 HResult = -2146232000
Message=Root element is missing. Message =根元素丢失。
Source=System.Xml 来源= System.Xml

app.config app.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MercazApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="MyKey"
         connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Taekwon-Do\MercazApp\DB\LoginDb.mdf;Integrated Security=True;"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <userSettings>
    <MercazApp.Properties.Settings>
      <setting name="username" serializeAs="String">
        <value />
      </setting>
      <setting name="password" serializeAs="String">
        <value />
      </setting>
      <setting name="name" serializeAs="String">
        <value />
      </setting>
    </MercazApp.Properties.Settings>
  </userSettings>
</configuration>

Root element is missing. 根元素丢失。 (C:\\Users\\alexs\\AppData\\Local\\MercazApp\\MercazApp.exe_Url_ka1yz14eyfxx0cttxywliv5ag3ke50uk\\1.0.0.0\\user.config) (C:\\ Users \\ alexs \\ AppData \\ Local \\ MercazApp \\ MercazApp.exe_Url_ka1yz14eyfxx0cttxywliv5ag3ke50uk \\ 1.0.0.0 \\ user.config)

seems relevant. 似乎相关。 Likely, that file changed to something bad. 该文件可能更改为错误的文件。 Inspect it. 检查一下。

I can't believe you! 我不敢相信你! I renamed that folder to old and after building it was created again and now it works!! 我将该文件夹重命名为old,在构建之后再次创建了该文件夹,现在可以正常使用了!! I spent 4 hours with that. 我花了四个小时。 Where did you see the error and why it caused? 您在哪里看到错误以及为什么导致错误?

I simply read the exception in full. 我只是完整地阅读了该异常。 Exceptions contain a lot of information. 异常包含很多信息。 It is worth acquiring a habit to fully read, understand and interpret it. 养成充分阅读,理解和解释它的习惯是值得的。

try putting the connection string directly into the code in place of the line that says: 尝试将连接字符串直接放入代码中,以代替表示以下内容的行:

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

This will tell you whether it's a web.config issue. 这将告诉您是否是web.config问题。 Since it started playing up suddenly, its quite likely that some config change in IIS triggered the web config to be updated. 由于它突然开始播放,因此IIS中的某些配置更改很可能触发了Web配置的更新。

also worth checking whther there are any other .config files in different folders (including app_data) if you find one with no content, just delete it. 同样值得检查的是,如果您发现其他文件夹中没有内容,则在其他文件夹(包括app_data)中是否还有其他.config文件,请将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM