Chris
Chris

Reputation: 2303

Getting initial catalog from the web.config file

I have a connection string in the web.config file. I have to get the database name from it. Let say my connection sting is

<add name="LocalSqlServer" connectionString="Data Source=XYZ;Initial Catalog=MyDataBase;Integrated Security=true" providerName="System.Data.SqlClient"/>

I want to get the database name [i.e. Initial Catalog] from the connection string.

How can I get it?

Upvotes: 30

Views: 13707

Answers (1)

marc_s
marc_s

Reputation: 755381

You can use the SqlConnectionStringBuilder for this purpose:

string connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);

string database = builder.InitialCatalog;

Upvotes: 53

Related Questions