开发者

Exception: type initializer for 'system.data.sqlclient.sqlconnection'?

开发者 https://www.devze.com 2023-03-25 17:46 出处:网络
I couldn\'t figure out what is the problem for this exception. The type initializer for \'System.Data.SqlClient.SqlConnection\' threw an exception

I couldn't figure out what is the problem for this exception.

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception

First attempt: I was using the WCF Service to make some small application. It works just fine and I can use the LINQ properly. After 2 or 3 days. Maybe after I close Visual studio and load the pro开发者_运维知识库ject again. The exception appear.

The 2nd attempt also the same. I create another project and it work just fine until I do something else (I do not change any code)

SQL Server working fine and I can connect through SQL Management Studio without problem.

A click at Debug error lead me to the connection string from linq file.

What could be the problem? I tried to search but couldn't find the answer to resolve this.

Thank you


This most likely means that there is an error in your app.config file, e.g. badly formed XML or unexpected elements. The error happens because the static fields inside SqlConnection read from app.config to determine trace detail level as described here: http://msdn.microsoft.com/en-us/library/ms254503.aspx


If you have a project that contains the NuGet packages for both Oracle and SQL Server, this solution might help you fix this error.

Uninstall both of these NuGet packages:

  • Oracle.ManagedDataAccess.Core
  • System.Data.SqlClient

Open your app.config and check if you still have have any dependent assembly entries listed. If you do, delete those entries now. They might look something like this:

<dependentAssembly>
    <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.19.1" newVersion="2.0.19.1" />
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="System.Data.SqlClient" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.1" newVersion="4.5.0.1" />
</dependentAssembly>

While still in your app.config file, delete the entire Oracle Managed Data Access section if it exists (This might be the only thing that you need to do to fix this problem, however we performed all of the steps listed in this solution). If you have it, it might look something like this:

<oracle.manageddataaccess.client>
    <version number="*">
      <settings>
          <!-- your TNS_ADMIN value would be located here -->
      </settings>
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) " />
      </dataSources>
    </version>
</oracle.manageddataaccess.client>

Open the packages.config file and double check to ensure that the entries for the Oracle and SQL Server packages are not still listed - they should be gone, but it doesn't hurt to be 100% sure.

Save your project, and then re-add the NuGet packages.


Delete app.config and then run the program


I also faced that exception once when I reopened my project. In my case the Config file had empty tag and key as you can see below. I removed the appSettings tag to fix the problem.

< ?xml version="1.0" encoding="utf-8" ?>

< configuration>

< appSettings>

< add key="" value="site"/>

< /appSettings>

< configSections>

< /configSections>

< /configuration>


Check if you have the following Windows hotfix installed - KB2840628 - for the .NET FW 4.0 (use Windows Control Panel->Programs+Features and click 'view installed updates') and if so try uninstalling it...

https://connect.microsoft.com/VisualStudio/feedback/details/793500/sqlclr-typeinitializationexception-in-sqlconnection-since-kb2840628

It resolved the problem for me.


In my case , the problem was modifying tag name in app.config file by mistake just compare your app.config file with another old one .. You will find out the problem ...


I have the same error. And I findout the solution for it by change "app.config":

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="O4E.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
  <applicationSettings>
    <O4E.Settings>
      <setting name="abc" serializeAs="String">
        <value>xyz</value>
      </setting>
    </O4E.Settings>
  </applicationSettings>
  <appSettings>
    <add key="" value=""/>
    <add key="" value=""/>
  </appSettings>
</configuration>

Note: Line [supportedRuntime version="v2.0.50727"] is the most importance.


I've read a lot about this issue, but no one has mentioned the following cause of this error.

Some of our users, who had an older version of VMWare View Client which was then upgraded, are now seeing this issue.

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception

  • Users who only had just the old version of VMWare View Client installed have no problem.
  • Users who only had just the new version of VMWare View Client installed have no problem.
  • But users who had an old version of VMWare View Client, but have now been upgraded, do have a problem, and it does continue even if we uninstall VMWare View Client, then install the new version.

We did attempt to reinstall .Net Framework and Visual Studio Tools for Office, but this didn't fix anything.

We haven't found a fix yet, but I hope this helps other users.

Btw, we did check our app.config's and web.config's, and these are all fine (and work perfectly with the rest of our users).

One other discovery:

  • If your user has local admin rights, this problem doesn't occur after upgrading VMWare View Client.
  • If they don't have local admin rights, the issue will occur.

So... we're now stuck.

I have a test laptop, and I can reproduce the error everytime when my user ID doesn't have Local Admin rights. If we then grant Local Admin rights, and I log in again, the error goes away.

And I added a load of try..catch's in my code, and found that the exception occurs in this one line of code:

MikesDataContext dc = new MikesDataContext()

So, it's actually making the connection which throws the exception. It does not get as far as the next lines of code which use this connection.


sorry for the late response on this, but after investigating this issue for almost 3 days in a row I thought I'd share my results. I had the same error pop up in SQL Server Reporting Services, more specifically the web portal after getting HTTP 503 Service Unavailable errors on my browsers.

Turns out that the issue was related to both, the service account that runs the SSRS instance plus the account that connects to the database, which in my case was located on another server than the reporting instance. I haven't yet been able to pinpoint exactly where the problem lies inside the configuration and/or user rights, i.e. where do the accounts exactly need elevated rights. All in all seems that the underlying problem was that the account I was running the web portal code under, didn't have enough rights to call on the System.Data.SQLClient type.

TL;DR: Check your users permissions! Hope this helps someone, I had a long 3 days trying to figure this out.


A similar solution to those mentioned above, but in my case it was the applicationSettings tag within the app.config that needed to be removed.


I have the same error and the problem was in Web.config. In my case, just add the connectionStrings tag and its value and problem solved.

<connectionStrings>
your connection strings
</connectionStrings> 


I have run across this issue on programs that work fine for months, but sudden stop working. The fix I have found works every time is to place this as the first call in your Main().

System.Configuration.ConfigurationManager.GetSection("system.xml/xmlReader");

0

精彩评论

暂无评论...
验证码 换一张
取 消