问题
I am working on the class which is not aware about the RDBMS being used. Of-course, rest of the application is well aware about it. Connection string is input to this class. I need database name.
How can I extract database name from connection string irrespective of RDBMS?
I read following question:
How to get Database Name from Connection String using SqlConnectionStringBuilder
Extract properties of sql connection string
Following approaches are suggested there:
Use Connection String Builder class:
Most answers use connection string builder with specific RDBMS. So, I have to use eitherSqlConnectionStringBuilder
orMySqlConnectionStringBuilder
etc. This is RDBMS specific. To make this cross-RDBMS, I must useDbConnectionStringBuilder
instead. Following is the code withDbConnectionStringBuilder
:DbConnectionStringBuilder builder = new DbConnectionStringBuilder(); builder.ConnectionString = connectionString; string databaseName = builder["Initial Catalog"] as string;
Problem with this approach is the key
"Initial Catalog"
used. As syntax of connection string is different for each RDBMS, connection string for other RDBMS may not contain the key and the code does not work.
For example, above code do not work for following valid MySql connection string because it does not contain the key.Server=MyServer;Database=MyDB;User ID=MyUser;Password=MyPassword;
Use Connection class:
Again, I have to useIDbConnection
interface orDbConnection
abstract class to make this cross-RDBMS. And I cannot create an instance of interface or abstract class. I have to use concrete class to instantiate it which again becomes RDBMS specific.String handling:
Parsing the string using string handling features of C# or regular expression or similar. As I said earlier, syntax of connection string varies per RDBMS, this does not work.
Other workaround I can think of is to use some brute force logic like checking multiple keys (Initial Catalog, Database
etc) instead of one. I want to avoid this if possible. These could be endless conditions and thus not a reliable way. If this is only the way to go, please suggest possible keys to check those cover all possible syntax of connection string for all RDBMS.
Note:
When I say "cross-RDBMS" or "all RDBMS", I do not actually mean "all RDBMS on earth". Solution should cover at-least three RDBMS (SQL Server, My SQL, Oracle) with their multiple variants/versions/engines.
Edit 1:
To resolve issue with connection string builder solution mentioned above, I have tried below:
DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
builder.ConnectionString = connectionString;
object dbName = null;
bool dbNameFound = false;
dbNameFound = builder.TryGetValue("Initial Catalog", out dbName);
if(dbNameFound == false)
dbNameFound = builder.TryGetValue("Database", out dbName);
if(dbNameFound == false)
throw new Exception("Failed to extract database name from connection string.");
string databaseName = Convert.ToString(dbName);
This code still does not work with few syntax of Oracle.
来源:https://stackoverflow.com/questions/47727524/how-to-extract-database-name-from-connection-string-irrespective-of-rdbms