System.IO.FileNotFoundException with SqlClient
Asked Answered
J

4

8

After fixing my problem as mentioned here I am getting the below exception

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

My library is a .NET Standard 1.4 and the WebApp is .NET Framework 4.6.1

System.Data.SqlClient is version - 4.3.0 NuGet package. So I tried doing the below but in vain:

<dependentAssembly>
      <assemblyIdentity name="System.Data.SqlClient" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
      <bindingRedirect oldVersion="4.1.0.0" newVersion="4.3.0.0"/>
</dependentAssembly>
Jog answered 20/4, 2017 at 14:6 Comment(5)
What is your connection string? The connection string should not include the mdf file. The server owns the mdf file and users do not normally have access to the file. Use the SQL Server name, instance and default database name in the connection string.Herc
@Herc theres no problem with db if I am using my console app to access data using the same settings and the connection string includes what you saidJog
Console may be running with different credentials than in SQL Server. The console interface may be ignoring the mdf file name which VS is giving error.Herc
@Herc where can I edit those settings ? WebConfig doesn't contain the db connection I have a different dll that is handling the CRUD operationsJog
Try loading latest package : nuget.org/packages/System.Data.SqlClient/4.3.0. It is in the constructor for SQLClient.Herc
S
17

I guess you may have figured it out already but hope it would save someone precious time

In order to make everything work you would need to reference System.Data.SqlClient in WebApp .NET Framework 4.6.1 project that is referencing your .NET Standard Library. After that everything should work just fine.

Sounds like .NET Standard Library haven't grabbed with itself dependent library binary. There is nothing like "Copy Local" option in .NET Standard references so I don't see any way to check or set this behavior too

Stanfield answered 2/9, 2017 at 16:32 Comment(0)
R
4

We resolved this by adding System.Data.SqlClient as a reference to our netstandard package, just using the nuget package manager. Once that was done Visual Studio did the rest in the Web solution.

It seems like the dependencies are not all added to the netstandard library when adding a package like Dapper.

Rudiment answered 12/7, 2018 at 15:40 Comment(0)
B
1

This sent me on a merry chase until I actually compared my project that didn't work, to one that did.

NUGET install for System.Data.SQLClient doesn't necessarily install the files of that name (.dll and .xml) - at least, it didn't for me.

I took those two files from the older project, put them in the bin for the newer, and this solved the problem.

Brister answered 25/3, 2020 at 15:30 Comment(1)
I just searched Nuget for System.Data.SqlClient and installed that and it magically started working, so same thing I guess, just different source for those files.Dumfound
M
0

What I tried:

I was also having the same error. I created a sample web api(2.0) which uses a .net standard 2.0 class library. In this library I am using Dapper and Dapper.Contrib and using their extension methods tried to query db. But was having the same error. I tried adding System.Data.SqlClient nuget package in the class library but it did not work. I am using VS2019 Community Edition. This has happened to me twice. So writing the exact scenario and solution that worked for me.

What Worked:

It was eventually resolved by removing System.Data.SqlClient nuget package from the class library and re-adding it in WebApi.

Which is strange since class library is able to resolve dapper nuget package but not System.Data.SqlClient, from within itself.

Mg answered 24/5, 2019 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.