Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 403
Chris Avatar asked Feb 24 '11 14:02

Chris


People also ask

What is initial catalog in connection string in web config?

What is initial catalog in connection string in web config? Initial Catalog – The name of the Database. User Id – The User Id of the SQL Server. Password – The Password of the SQL Server.

What is SQL initial catalog?

Accepted Answer. Initial Catalog is the name of the database to be used by the connection string, which is located on the server that was specified in the Data Source part of the connection string.

What is the difference between initial catalog and database?

The only difference is the name. These can be used interchangeably.

What is connection string providerName?

The providerName attribute is used to set the name of the .NET Framework data provider that the DataSource control uses to connect to an underlying data source. If no provider is set, the default is the ADO.NET provider for Microsoft SQL Server.


1 Answers

You can use the SqlConnectionStringBuilder for this purpose:

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

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);

string database = builder.InitialCatalog;
like image 85
marc_s Avatar answered Oct 04 '22 04:10

marc_s