How to get Database Name from Connection String using SqlConnectionStringBuilder

C#asp.netSqlado.net

C# Problem Overview


I do not want to split connection strings using string manipulation functions to get Server, Database, Username, and Password.

I read the following link and read the accepted answer, I found that is the best way to get username and password out from connection string, but what about Database Name?

https://stackoverflow.com/questions/9975122/right-way-to-get-username-and-password-from-connection-string

How to get Database Name from Connection String using SqlConnectionStringBuilder. (does the DataSource is the Server name?)

C# Solutions


Solution 1 - C#

You can use the provider-specific ConnectionStringBuilder class (within the appropriate namespace), or System.Data.Common.DbConnectionStringBuilder to abstract the connection string object if you need to. You'd need to know the provider-specific keywords used to designate the information you're looking for, but for a SQL Server example you could do either of these two things:

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);

string server = builder.DataSource;
string database = builder.InitialCatalog;

or

System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();

builder.ConnectionString = connectionString;

string server = builder["Data Source"] as string;
string database = builder["Initial Catalog"] as string;

Solution 2 - C#

See MSDN documentation for InitialCatalog Property: > Gets or sets the name of the database associated with the connection... > This property corresponds to the "Initial Catalog" and "database" keys within the connection string...

Solution 3 - C#

A much simpler alternative is to get the information from the connection object itself. For example:

IDbConnection connection = new SqlConnection(connectionString);
var dbName = connection.Database;

Similarly you can get the server name as well from the connection object.

DbConnection connection = new SqlConnection(connectionString);
var server = connection.DataSource;

Solution 4 - C#

string connectString = "Data Source=(local);" + "Integrated Security=true";

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);

Console.WriteLine("builder.InitialCatalog = " + builder.InitialCatalog);

Solution 5 - C#

this gives you the Xact;

System.Data.SqlClient.SqlConnectionStringBuilder connBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();

connBuilder.ConnectionString = connectionString;

string server = connBuilder.DataSource;           //-> this gives you the Server name.
string database = connBuilder.InitialCatalog;     //-> this gives you the Db name.

Solution 6 - C#

Database name is a value of SqlConnectionStringBuilder.InitialCatalog property.

Solution 7 - C#

You can use InitialCatalog Property or builder["Database"] works as well. I tested it with different case and it still works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionImran RizviView Question on Stackoverflow
Solution 1 - C#Romil Kumar JainView Answer on Stackoverflow
Solution 2 - C#unarityView Answer on Stackoverflow
Solution 3 - C#nawfalView Answer on Stackoverflow
Solution 4 - C#Kishore KumarView Answer on Stackoverflow
Solution 5 - C#pvaju896View Answer on Stackoverflow
Solution 6 - C#DennisView Answer on Stackoverflow
Solution 7 - C#HabibView Answer on Stackoverflow