DbProviderFactory CreateConnection失败System.Data.SqlClient

时间:2018-08-15 14:13:43

标签: vb.net

这是要测试的代码。创建一个新窗体,添加一个按钮,并将此代码放在按钮中。

    Dim dbf As Data.Common.DbProviderFactory
    '
    ' uncomment one of the below dbf = 
    '
    'this fails
    'dbf = Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient")
    'these work
    'dbf = Data.Common.DbProviderFactories.GetFactory("System.Data.Odbc")
    'dbf = Data.Common.DbProviderFactories.GetFactory("System.Data.OleDb")

    Dim conn As Data.Common.DbConnection
    conn = dbf.CreateConnection

创建连接仅对SqlClient失败。错误是

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.SqlClientFactory.CreateConnection()
       at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\dewayne.basnett\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:line 19
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.PerformClick()
       at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
       at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
       at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
       at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
       at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
       at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=Failed to read the configuration section for enclave providers. Make sure the section is correctly formatted in your application configuration file. Error Message: Configuration system failed to initialize
       Source=System.Data
       StackTrace:
            at System.Data.SqlClient.SqlConnection..cctor()
       InnerException: System.Configuration.ConfigurationErrorsException
            BareMessage=Configuration system failed to initialize
            HResult=-2146232062
            Line=0
            Message=Configuration system failed to initialize
            Source=System.Configuration
            StackTrace:
                 at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
                 at System.Configuration.Internal.InternalConfigRoot.OnConfigRemoved(InternalConfigEventArgs e)
                 at System.Configuration.Internal.InternalConfigRoot.RemoveConfigImpl(String configPath, BaseConfigurationRecord configRecord)
                 at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
                 at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
                 at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
                 at System.Data.SqlClient.SqlConnection..cctor()
            InnerException: System.Configuration.ConfigurationErrorsException
                 BareMessage=Unrecognized configuration section userSettings.
                 Filename=C:\Users\dewayne.basnett\AppData\Local\WindowsApplication1\WindowsApplication1.vshos_Url_odwpvb4iq0tpdwxwwhwkttkzifz10zja\1.0.0.0\user.config
                 HResult=-2146232062
                 Line=3
                 Message=Unrecognized configuration section userSettings. (C:\Users\dewayne.basnett\AppData\Local\WindowsApplication1\WindowsApplication1.vshos_Url_odwpvb4iq0tpdwxwwhwkttkzifz10zja\1.0.0.0\user.config line 3)
                 Source=System.Configuration
                 StackTrace:
                      at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
                      at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
                      at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
                      at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
                 InnerException: 

编辑:配置文件是由IDE创建的,但这里是FWIW

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <WindowsApplication1.My.MySettings>
            <setting name="AutoIncrement" serializeAs="String">
                <value>False</value>
            </setting>
        </WindowsApplication1.My.MySettings>
    </userSettings>
</configuration>

App.config内容

    'FAILING 
    '<?xml version="1.0" encoding="utf-8"?>
    '<configuration>
    '    <startup>
    '        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    '    </startup>
    '</configuration>

    'WORKING
    '<?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="WindowsApplication1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
    '        </sectionGroup>
    '    </configSections>
    '    <startup>
    '        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    '    </startup>
    '    <userSettings>
    '        <WindowsApplication1.My.MySettings>
    '            <setting name="foo" serializeAs="String">
    '                <value/>
    '            </setting>
    '        </WindowsApplication1.My.MySettings>
    '    </userSettings>
    '</configuration>

1 个答案:

答案 0 :(得分:0)

答案是,我不确定这个背后的机制是...

昨天,另一个域中的用户试图安装我的一个应用程序,安装程序抛出错误。它看起来像一个许可问题,但他们坚持认为不是。他们的一位程序员建议我将代码文件的属性从不复制更改为始终复制。我认为这不会有所帮助,但他们坚持认为。最终,我发现它们是如何映射到安装目录的。

他们将其作为\ fileserver.mo.gov \ install。将其更改为\ fileserver \ install可解决此问题。我没有将代码文件改回来。

随着这个问题变得更加奇怪,我想到了昨天发生的事情,并想起了这一变化。我将其设置回“请勿复制”,重新发布了该应用程序,以上问题消失了。正如我说的那样,我不了解其背后的机制,但我的问题已解决。