This topic is locked

How to find out your current connection string

1/16/2019 1:15:57 PM
ASPRunner.NET Tips and tricks
admin

In case you need to open a connection manually or just need to know what database you currently working with.

  1. Proceed to output folder and open web.config file in text editor. You will find <connectionStrings> section there:

<connectionStrings>

<add name="livedemo_northwind_at_localhost" connectionString="Server=localhost;Database=livedemo_northwind;User Id=root;Password=" />

</connectionStrings>


Notice the name of connection string: "livedemo_northwind_at_localhost"
2. Here is the code that reads connection string and prints it on the page. Replace "livedemo_northwind_at_localhost" with your connection string name.

System.Configuration.Configuration rootWebConfig =

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");

System.Configuration.ConnectionStringSettings connString;

if ( rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0) {

connString = rootWebConfig.ConnectionStrings.ConnectionStrings["livedemo_northwind_at_localhost"];

if ( connString != null) {

MVCFunctions.Echo(connString.ConnectionString);

}

}
Pete K 1/17/2019

Thanks for this helpful tip, Sergey. I like to include the name of the database, along with app version and contact info in my enterprise apps for support purposes. This will avoid having to hard code the name of the database and remember to update it when we do updates.