ASP.NET Tutorial/ADO.net Database/Connection Strings
Add the connection string to a web configuration file higher in the folder hierarchy
<source lang="csharp">
The root Web.Config file is located at the following path: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG</source>
Encrypting Connection Strings with aspnet_regiis located in C:\WINDOWS\Microsoft.NET\Framework\v27\
<source lang="csharp">
aspnet_regiis -pef connectionStrings "c:\Websites\MyWebsite"</source>
Storing Connection Strings in the Web Configuration File
<source lang="csharp">
<configuration>
<connectionStrings> <add name="Products" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" /> </connectionStrings>
</configuration> You can add as many connection strings to the connectionStrings section as you want. <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Show Products</title>
</head> <body>
<form id="form1" runat="server">
<asp:GridView id="grdProducts" DataSourceID="srcProducts" Runat="server" /> <asp:SqlDataSource id="srcProducts" SelectCommand="SELECT * FROM Products" ConnectionString="<%$ ConnectionStrings:Products %>" Runat="server" />
</form>
</body> </html></source>