Search code examples
.netparsingconnection-stringdbconnectiondbproviderfactories

How do I detect DbConnection type name from Connection string only


Is there new or existing method to parse a connection string’s keys to determine the connection type (OleDbConnection, OdbcConnection, SqlConnection, MySqlConnection, DB2Connetrion, etc) ?

It needs to be using either .NET Standard or .NET Core or .NET 5, not .Net Framework.

The result returned should be either a string of the name or an enum of connection types parsed names, not an actual DbConnection.

Was thinking of using DbConnectionStringBuilder and if it contains key:

  • “provider” return “OleDbConnection”.
  • “driver” return “OdbcConnection”.

It’s all the other connection string providers I need help identifying.


Solution

  • I don't know why you need to identify which provider a connection string corresponds to. If it's to automatically guess which provider to use you should probably consider supplying both a connection string and a provider explicitly. Guessing the provider based solely on the connection string is just that: a guess. It can not be a foolproof solution.

    That being said, here are some pointers to common ADO.NET providers with the list of keywords used in their connection strings.

    From there, you can extract all the keywords and write a heuristic that computes what is the most likely provider for a given connection string.