3

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:

  1. Use Connection String Builder class:
    Most answers use connection string builder with specific RDBMS. So, I have to use either SqlConnectionStringBuilder or MySqlConnectionStringBuilder etc. This is RDBMS specific. To make this cross-RDBMS, I must use DbConnectionStringBuilder instead. Following is the code with DbConnectionStringBuilder:

    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;
    
  2. Use Connection class:
    Again, I have to use IDbConnection interface or DbConnection 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.

  3. 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.

12
  • Database or Initial Catalog should be synonyms. At least looking at the ConnectionString property docs. Did you try to check if, with different db, you get the same values in both properties? Commented Dec 9, 2017 at 10:31
  • @Steve: "same values in both properties". Properties of which class? Only RDBMS-specific classes expose those as properties. With DbConnectionStringBuilder, I have to work like key-value pair as the code shows in question. Commented Dec 9, 2017 at 10:33
  • I'm not quite following your problem here. You have a connection string. What use is that connection string if you don't know what concrete RDBMS it belongs to? How do you actually get a valid DbConnection out of it? If you know how to do that, you should know how to get the database name too. Commented Dec 9, 2017 at 10:42
  • I see, but what I am trying to say is: There are just two possibilities for that. There is initial catalog or database. So with if(string.IsNullOrEmpty(result)) result = build["database"]; Testing now with MySql Commented Dec 9, 2017 at 10:42
  • 1
    And I cannot create an instance of interface or abstract class. - So what's the input you have? Only connection string? In that case try this answer: stackoverflow.com/a/15529085/661933 (a very simple and limited parser I wrote. Extending it wont be difficult). Commented Dec 11, 2017 at 5:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.