<p>I usually store my connectionstring in web.config or in the application settings of my Visual Studio project. The application I'm currently working on makes a lot of trips to the database which means it will look up the connectionstring every time. Should I be putting the connectionstring in the cache or should I be looking at storing the whole SqlConnection object in the cache to eliminate the need to open and close them all the time?</p>

<p><strong>Update</strong>: Seems like the consensus is to store the connection string in a configuration file and leave the caching in the trusting hand of ADO.NET</p>

[{'answer_id': 41053, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 0, 'selected': False, 'text': '<p>Keep it in a configuration file.  Use a robust data access strategy provided by tools like NHibernate or Linq to Sql.</p>\n'}, {'answer_id': 41054, 'author': 'Mark Glorie', 'author_id': 952, 'author_profile': 'https://Stackoverflow.com/users/952', 'pm_score': 0, 'selected': False, 'text': '<p>From what I can recall the contents of the .config file are held in memory anyway... I\'ll get back to you.  </p>\n\n<p>Edit: What <a href="https://stackoverflow.com/questions/41050/where-do-you-store-your-connectionstring-should-it-be-cached#41055">HE</a> said</p>\n'}, {'answer_id': 41055, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 3, 'selected': True, 'text': "<p>I wouldn't cache the connection object, that will defeat the built-in connection pooling -- ADO.NET will handle connections (assuming you instantiate and close them) efficiently by itself.</p>\n\n<p>As far as the connection string itself, you shouldn't need to cache it if you load it from connection -- the connection manager object in the .NET 2.0 framework loads the config into memory when you first access it, so there are no repeat trips to the file system.</p>\n"}, {'answer_id': 41058, 'author': 'Kevin Kershaw', 'author_id': 4292, 'author_profile': 'https://Stackoverflow.com/users/4292', 'pm_score': 1, 'selected': False, 'text': '<p>I usually cache the connection string in a global configuration object in my application.  This value is loaded up at the beginning of program execution from where ever it is stored -- file, encrypted file, config file, etc.  ADO.NET is very good at caching connection objects to the database so I would not cache the SqlConnection object.</p>\n'}, {'answer_id': 41062, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 2, 'selected': False, 'text': "<p>The web.config is cached.  But even if it wasn't, don't forget that ado.net maintains a connection pool - its not opening a new connection every time you make a call to the db.</p>\n"}, {'answer_id': 2168892, 'author': 'Yordan Georgiev', 'author_id': 65706, 'author_profile': 'https://Stackoverflow.com/users/65706', 'pm_score': 0, 'selected': False, 'text': '<p>A possible solution: \nStore the initial encrypted connection string ( in Web.Config or App.Config) for a login allowed to run only one stored procedure for authentication. Than switch the login dynamically from encrypted values stored in a config table in the db. </p>\n'}]
['https://Stackoverflow.com/questions/41050', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/202/']