Forums: How to find out your current connection string - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to find out your current connection string

#1 User is offline   admin 

  • Administrator
  • PipPipPip
  • Group: Admin
  • Posts: 16224
  • Joined: 03-February 03
  • Gender:Male

Posted 16 January 2019 - 06:15 PM

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);
	}
}

Best regards,
Sergey Kornilov
0

#2 User is offline   Pete K 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 236
  • Joined: 05-February 16
  • Gender:Male

Posted 17 January 2019 - 12:10 PM

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.


----------------------------------------------------------
Pete K.
Lead Web Developer
Fayette County Public Schools | Lexington, Kentucky, USA
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users