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?
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.
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.
The only difference is the name. These can be used interchangeably.
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.
You can use the SqlConnectionStringBuilder
for this purpose:
string connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
string database = builder.InitialCatalog;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With