115

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?

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?)

8 Answers 8

179

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;
Sign up to request clarification or add additional context in comments.

2 Comments

For me, the last line needed to be: string database = builder["Initial Catalog"] as string; - "Database" was an invalid keyword.
@Sandra yes ur correct. builder["Database"] as string will work if we use SqlConnectionStringBuilder .
55

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

Comments

42

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;

5 Comments

This becomes specific for SqlConnection. Is there any similar way out to make this cross RDBMS?
@AmitJoshi Shouldnt they all implement IDbConnection?
@nawfal: I agree but that still does not make it usable. I have explained this in details in this question. stackoverflow.com/q/47727524/5779732
It can get Database and the DataSource, but couldn't get the userId and the password in this way.
13
string connectString = "Data Source=(local);" + "Integrated Security=true";

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);

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

Comments

7

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.

Comments

5

Database name is a value of SqlConnectionStringBuilder.InitialCatalog property.

Comments

5

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

Comments

2

If you are using .Net EF Core

using (var dbContext = new AppDbContext(serviceProvider.GetRequiredService<DbContextOptions<AppDbContext>>()))
{
  var dbConnection = dbContext.Database.GetDbConnection();

  //var serverName = dbConnection.DataSource;
  //var databaseName = dbConnection.Database;
}

Comments

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.