未识别的配置部分 connectionStrings。

4

我一直在VS2005上开发一个项目,该项目使用本地连接到Access数据库。

在过去的一周里,我安装了.NET Framework 3.5以用于另一个项目以及VS6。

我回到我的VS2005应用程序,突然出现了大问题:

任何使用我的OLEDB连接的类的设计器都显示如下内容:

无法将类型为“System.Configuration.DefaultSection”的对象转换为类型“System.Configuration.ConnectionStringsSection”。 隐藏

在 System.Configuration.Configuration.get_ConnectionStrings(),Microsoft.VisualStudio.Shell.Design.Serialization.ConfigurationHelperService.ReadConnectionStrings(String configFileName, DocData configDocData, String prefix), Microsoft.VisualStudio.Editors.SettingsDesigner.AppConfigSerializer.Deserialize(DesignTimeSettings Settings, String SectionName, DocData AppConfigDocData, MergeValueMode mergeMode, IUIService UIService),Microsoft.VisualStudio.Editors.SettingsGlobalObjects.SettingsFileGlobalObject.LoadSettings(String fileName),Microsoft.VisualStudio.Editors.SettingsGlobalObjects.SettingsFileGlobalObject.BuildType(), Microsoft.VisualStudio.Editors.SettingsGlobalObjects.SettingsFileGlobalObject.GetObjectType(), Microsoft.VisualStudio.Shell.Design.GlobalType.get_ObjectType(),Microsoft.VisualStudio.Shell.Design.GlobalObject.GetHashCode(), Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GlobalKey.GetHashCode(), System.Collections.Generic.ObjectEqualityComparer1.GetHashCode(T obj),System.Collections.Generic.Dictionary2.FindEntry(TKey key), Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GetGlobalObjects(Type baseType), Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GetGlobalObjects(), Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetTypeFromGlobalObjects(String name, Boolean throwOnError, Boolean ignoreCase), Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase), Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError),System.ComponentModel.Design.Serialization.CodeDomSerializerBase.GetType(ITypeResolutionService trs, String name, Dictionary2 names), System.ComponentModel.Design.Serialization.CodeDomSerializerBase.FillStatementTable(IDesignerSerializationManager manager, IDictionary table, Dictionary2 names, CodeStatementCollection statements, String className), System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration), System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager), Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager), Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)

当我运行应用程序时,启动时遇到以下异常:

"未识别的配置节 connectionStrings。"

在网上搜索时,这些问题似乎通常与VS2005构建的内容和部署在.net 1.1框架上有关;但是,这些内容都在VS中作为Windows表单应用程序本地运行(而不是在IIS上)。我尝试卸载并重新安装VS2K5,但没有效果。

有什么想法吗? 谢谢, 马特

4个回答

8

我知道这是一个相当古老的帖子,但最近我也遇到了同样的问题,我通过添加:

 <configSections>
    <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
    <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>
 <configSections>

我的连接字符串之前:

    <connectionStrings>
        <add name="XXX" connectionString="Data Source=.;Initial Catalog=db;User Id=user;Password=pw;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

因此,我的app.config看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
     <configSections>
        <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
        <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>
     <configSections>   
    <connectionStrings>
        <add name="XXX" connectionString="Data Source=.;Initial Catalog=db;User Id=user;Password=pw;" providerName="System.Data.SqlClient"/>
      </connectionStrings>
</configuration>

在C#中,我使用以下代码来检索我的连接字符串:
ConfigurationFileMap fileMap = new ConfigurationFileMap("myApp.config"); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
this.connectionString = configuration.ConnectionStrings.ConnectionStrings["XXX"].ConnectionString;

这对我有用,尽管appSettings部分不是必需的(除非你在配置中使用它)。 - MatthewC

6

我猜测可能是因为在web.config文件的<configSections>元素之前有其他内容,例如:

<configSections>元素之前添加以下内容会导致错误:

<configuration>
  <connectionStrings [...] />
  <configSections>
    <sectionGroup name=[...] />
  </configSections>
</configuration>

<configSections>元素需要放在第一位:


该元素必须作为配置文件的第一个元素出现。
<configuration>
  <configSections>
    <sectionGroup name=[...] />
  </configSections>
  <connectionStrings [...] />
</configuration>

也许不是导致您问题的原因,但我想提一下它...

谢谢回复! 我不认为这是问题,我的app.config如下:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="Mockups.Properties.Settings.MockupDSConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MockupDS.mdb" providerName="System.Data.OleDb" /> </connectionStrings> </configuration> - Matty U
我想有些配置出了问题 - 不幸的是,卸载/安装东西似乎不能解决它。即使我尝试在连接向导中创建新的DataSource连接 - 选择OLE DB连接,选择.mdb并单击“确定”,我会得到一个弹出窗口,显示“已由调用目标引发异常。”就是这样!非常令人沮丧... - Matty U
我曾经为了弄清楚为什么我的App.config对于某些自定义类型无法正常工作而苦苦思索,而这就是问题所在。谢谢! - dmo

0

我曾经遇到过同样的问题,请检查您的 machine.config 文件。

<configuration>
 <system.windows.forms jitDebugging="true"/>
 <configSections>
  <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
  <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>

当我移除了

..system.windows.forms jitDebugging="true"/>

后,它又正常工作了。


0

在我的情况下,这是camelCase问题。 <connectionStrings><connectionstrings>都可以工作,但在部分名称和连接字符串本身中应该是相同的。

  <connectionStrings>
    <add name="default"
         connectionString=[...] />
  </connectionStrings>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接