Krille
Krille

Reputation: 371

Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0

First of, some context information:

The platform this is running on has .Net Framework 4.7.1 installed. I have a Class Library that is in the .Net Standard 2 specification in order to support .Net Core in the future. Now parts of dependencies, namely Dapper, uses System.Data.SqlClient. This library works just fine on my own machine but I run into problems when I deploy and test it on my Windows 2012 server. Namely, I have a runtime error when Dapper is used: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=kfddsnfsjnfs' or one of its dependencies. The system cannot find the file specified.

Mind you I first had version 4.5.1.0 installed. I then downgraded to 4.4.0.0 and rerrun the code. Now I got the same error but this time regarding 4.2.0.0. But I cannot seem to find this particular version on Nuget. After this I googled. A lot. First I tried adding rebindining the old version with a new by adding both a

appsettings.json:

{
  "dependentAssembly": {
    "assemblyIdentity": {
      "name": "System.Data.SqlClient",
      "publicKeyToken": "kfddsnfsjnfs",
      "culture": "neutral"
    },
    "bindingRedirect": {
      "oldVersion ": "4.4.0.0",
      "newVersion": "4.5.1"
    }
  }
}

and app.config:

<dependentAssembly>
  <assemblyIdentity name="System.Data.SqlClient"  publicKeyToken="kfddsnfsjnfs"  culture="neutral" />
  <bindingRedirect oldVersion="4.4.0.0" newVersion="4.5.1.0" />
</dependentAssembly>

However it didn't make a difference. I have also tried older versions of the SqlClient and multiple reinstalls. I also found people who said to double check the csproj file so it didn't reference something in the gac, but it does not:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Authors>me</Authors>
    <Product />
    <Company />
    <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
    <Version>1.0.8</Version>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="TaskMetadata.json">
      <PackagePath>TaskMetadata.json</PackagePath>
      <Pack>True</Pack>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="dapper" Version="1.50.5" />
    <PackageReference Include="itextsharp" Version="5.5.13" />
    <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="External\" />
  </ItemGroup>

</Project>

Upvotes: 37

Views: 105577

Answers (8)

Petter Ivarsson
Petter Ivarsson

Reputation: 703

I was getting this error at the console in one of my webjobs in Azure. In the publish profile I changed configuration from "debug" to "release" and then it worked.

Upvotes: 1

user2565663
user2565663

Reputation: 1238

Switch from System.Data.SqlClient to Microsoft.Data.SqlClient.

https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/

Upvotes: 10

cooplarsh
cooplarsh

Reputation: 51

I was getting this error with .NET Core (using the latest Microsoft.Data.SqlClient) and had to change my DBProvider from

Microsoft.Data.SqlClient

to

Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient

in order for NLog to load Microsoft.Data.SqlClient

Upvotes: 0

hassan kamarzadeh
hassan kamarzadeh

Reputation: 362

install version 4.8.2 in both project

Install-Package System.Data.SqlClient -Version 4.8.2

Upvotes: 11

Gardy
Gardy

Reputation: 51

I had the same issue, Basically I was developing a win form app. with .net framework, and my class library was .net standard.

I tried install and uninstall System.Data.SqlClient ten times, it did not work

Then something later crossed my mind as I installed same reference in the UI layer as well then it worked. even though, I did not have any code talking to Sql on UI layer, then another issue raised as the UI did not recognize reference to Dapper as well, and I had to install Dapper reference on UI too. Then everything was fine.

I really surprises how that did not work. Once you referenced the UI to the class library, the class library supposed to do what is required and pass the data to UI!!

Upvotes: 5

Sumit Gupta
Sumit Gupta

Reputation: 785

I was facing this issue while running unit test cases. {"Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.":"System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"} System.IO.FileNotFoundException

When I install System.Data.SqlClient, Version=4.4.0.0 then it did not work.

But this got fixed when I installed the latest version of System.Data.SqlClient nuget package.

Upvotes: 1

Farshid
Farshid

Reputation: 5364

Try adding System.Data.SqlClient again via NuGet although you may already have it handy as a part of the .NET Framework. This solution resolved my problem.

Upvotes: 4

Gabriel Luci
Gabriel Luci

Reputation: 40998

Each library runs under the process of the main application. So the main application needs to know to load the SqlClient DLL. So the parent project (the .NET 4.7.1 project) needs to have SqlClient added as a reference, either by installing the NuGet package, or just adding a reference by browsing to the DLL under the .NET Standard project.

Old answer: That version exists in NuGet: https://www.nuget.org/packages/System.Data.SqlClient/4.4.0

In the Package Manager Console (make sure the 'Default project' drop-down is set to the right one), try uninstalling and then reinstalling that specific version:

Uninstall-Package System.Data.SqlClient
Install-Package System.Data.SqlClient -Version 4.4.0

Update: Or, in your binding redirect, just use 4.2.0.0 as the oldVersion.

Upvotes: 51

Related Questions