ASP.NET Tutorial/ADO.net Database/Connection Strings

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add the connection string to a web configuration file higher in the folder hierarchy

The root Web.Config file is located at the following path:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG


Encrypting Connection Strings with aspnet_regiis located in C:\WINDOWS\Microsoft.NET\Framework\v27\

aspnet_regiis -pef connectionStrings "c:\Websites\MyWebsite"


Storing Connection Strings in the Web Configuration File

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