BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
Yes, ASP.NET provides DataSource controls, but wizards only get you so far. Share the secret of how Wise Owl create all our ASP.NET websites, using a single, simple data class.
- Data Classes in ASP.NET
- Our Example for this Data Classes Tutorial
- Storing and Retrieving Connection Strings (this blog)
- ADO.NET: DataSets, DataTables and Data Adapters
- Consuming our Data Classes
- A Class to Run a Stored Procedure and Return its Rows
- Consuming our Data Class on the Web Page
Posted by Andy Brown on 07 August 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Storing and Retrieving Connection Strings
A connection string tells your web page three things:
- Where to find the database you're trying to connect to;
- What format this database is in; and
- What it's called.
Storing Connection Strings
In ASP.NET 4.0 the best place to store them (as recommended by Microsoft) is in the connectionStrings section of the web.config file. Here's an example:
Here there is one connection string stored in the web.config file.
This connection string will connect to the SQL Server database called Movies on the server called XXX, using Windows authentication.
For everything you could conceivably want to know about connection strings, go to http://www.connectionstrings.com. The tagline is irresistible: "Forgot that connection string? Get it here!".
Getting at Connection Strings in Code
The modern way to retrieve a connection string from a web.config file is via a ConfigurationManager object. This is typical .NET gobbledegook; all you need to know is that the following code will give you the connection string called csMovies in your web.config file:
Later in this blog we'll use this to get the connection string to use to access data for our web page.
Having learnt how to get at connection strings to connect to a database, the next thing is to understand about data tables, datasets and data adapters.