Search code examples
c#.netsql-serverado.netsqlclient

Convert Microsoft.Data.SqlClient connections, commands, parameters etc. to System.Data.SqlClient


Last year, Microsoft introduced the new Microsoft.Data.SqlClient.

Now, if I wanted to write library code that takes for example a SqlConnection, how do I approach this so that it works for both Microsoft.Data.SqlClient.SqlConnection and the old System.Data.SqlClient.SqlConnection? Is there a built-in way, or a third party package, that can bridge the gap between the two SqlClient packages? Or do I have to double my library code, to be able to support both packages?


Solution

  • I don't see why you need to duplicate any code. The queries remain the same, just some additional features such as Azure Active Directory integration are available.

    Use DbConnection, DbCommand etc as base classes, and maybe use Dependency Injection or a similar technique to decide which type to use.