0

I was reading the answer to this question about the Microsoft.Data.SqlClient and System.Data.SqlClient which states that "Mixing up data providers isn't fun and should be avoided when possible." I'd like to know what kind of problems could be caused by this?

There are two scenarios I've considered, the first is referencing both packages directly in a project. I can see issues sharing a SqlConnection in this instance, or issues passing different implementations of SqlParameter, but am not sure what else would be painful.

The second is referencing one package, but having a reference to the other via a 3rd party dll. Could problems be caused in this latter case, and if so how?

My secret motivation for this is that we are experiencing odd behaviour that looks like connection leaks in our Hangfire service, and I am wondering if maintaining references to both the Microsoft.Data.SqlClient and System.Data.SqlClient packages is a possible cause.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
EyreCraggs
  • 51
  • 1
  • 5
  • 1
    "Not fun" should be taken quite literally here -- the issues you get from referencing both packages are annoyances, like having to catch two exception types if code could conceivably use both, ensuring the correct DB provider is resolved for code that does such things at runtime, resolving `Sytem.Data.SqlType` classes, etc. Actual problems from the different packages both running database code are not to be expected, as they don't share resources beyond the basics, and thus it should be much like (e.g.) using a provider for MySQL and SQL Server in the same application. – Jeroen Mostert Mar 14 '23 at 10:21
  • If you really wanted to know if the "odd behavior" is due to this, the best and most expedient way is to test it by eliminating one of them from the equation. If this isn't possible because it's an integral part of your setup, then you won't be able to forego a detailed analysis of the "odd behavior" anyway, and you'll discover whether or not it's related in the process. – Jeroen Mostert Mar 14 '23 at 10:22
  • It's like wiring up a workshop with both 110V and 240V... you'll have to take real care that you're connecting up the right wires, or you'll end up getting big issues and having to redo a lot of work. – JHBonarius Mar 14 '23 at 10:28
  • 1
    One immediate consequence will be even though you have `SqlConnection` in ilk in both assemblies, they really are different types and will be treated as such by the CLR. You cannot open connection from one package and expect to configure parameters from the other one. There might be implementation level assumptions too which may or may not get violated. – Tanveer Badar Mar 14 '23 at 10:34

0 Answers0