Storing Connection Strings in the Web Configuration File : Connection Strings « ADO.net Database « ASP.NET Tutorial






<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">
    <div>

    <asp:GridView
        id="grdProducts"
        DataSourceID="srcProducts"
        Runat="server" />

    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT * FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />

    </div>
    </form>
</body>
</html>








18.37.Connection Strings
18.37.1.Storing Connection Strings in the Web Configuration File
18.37.2.Add the connection string to a web configuration file higher in the folder hierarchy
18.37.3.Encrypting Connection Strings with aspnet_regiis located in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\