Share via


ConnectionStringsExpressionBuilder 類別

定義

擷取 (或產生程式碼以擷取) Web.config 檔案之 <connectionStrings> 區段的值。

public ref class ConnectionStringsExpressionBuilder : System::Web::Compilation::ExpressionBuilder
public class ConnectionStringsExpressionBuilder : System.Web.Compilation.ExpressionBuilder
type ConnectionStringsExpressionBuilder = class
    inherit ExpressionBuilder
Public Class ConnectionStringsExpressionBuilder
Inherits ExpressionBuilder
繼承
ConnectionStringsExpressionBuilder

範例

下列程式碼範例示範如何以宣告方式使用 SqlDataSource 控制項,從執行Microsoft SQL Server的電腦擷取資料,並將其顯示在 控制項中 GridView

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:SqlDataSource>

      <asp:GridView
          id="GridView1"
          runat="server"
          DataSourceID="SqlDataSource1">
      </asp:GridView>

    </form>
  </body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:SqlDataSource>

      <asp:GridView
          id="GridView1"
          runat="server"
          DataSourceID="SqlDataSource1">
      </asp:GridView>

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

程式碼會從Web.config檔案擷取下列連接字串。

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<connectionStrings>

<add name="MyNorthwind" connectionString="Data

Source=localhost;Integrated Security=SSPI;Initial

Catalog=Northwind;" providerName="System.Data.SqlClient" />

</connectionStrings>

</configuration>

備註

類別 ConnectionStringsExpressionBuilder 提供Web.config檔案區段中值的 <connectionStrings> 存取權。 <connectionStrings>Web.config檔案的 區段包含名稱/值組中的連接字串值。

藉由指派表單的運算式來擷取連接字串值

<%$ ConnectionStrings: secureConnectionString %>

至控制項屬性。 冒號之前的運算式部分 (:) 會指定要擷取的運算式類型,而冒號之後的部分表示連接字串的名稱。 上述運算式會從 Web.config 檔案擷取下列值。

<connectionStrings>

<add name="secureConnectionString" connectionString="Data

Source=localhost;Integrated Security=SSPI;Initial

Catalog=Northwind;" providerName="System.Data.SqlClient" />

</connectionStrings>

當頁面剖析器遇到前置詞為 ConnectionStrings 的運算式時,它會建立 類別的 ConnectionStringsExpressionBuilder 實例來處理運算式。

如果在將編譯的頁面中遇到運算式,物件 ConnectionStringsExpressionBuilder 會產生程式碼,從Web.config檔案擷取指定的連接字串。 如果在不會編譯的頁面中遇到運算式,當剖析頁面時, ConnectionStringsExpressionBuilder 物件會從Web.config檔案傳回值。

建構函式

ConnectionStringsExpressionBuilder()

初始化 ConnectionStringsExpressionBuilder 類別的新執行個體。

屬性

SupportsEvaluate

傳回值,指出是否可在未編譯的頁面中評估運算式。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
EvaluateExpression(Object, BoundPropertyEntry, Object, ExpressionBuilderContext)

傳回 Web.config 檔案之 <connectionStrings> 區段的值。

GetCodeExpression(BoundPropertyEntry, Object, ExpressionBuilderContext)

傳回要在頁面剖析期間評估的程式碼運算式。

GetConnectionString(String)

傳回 Web.config 檔案之 <connectionStrings> 區段的連接字串。

GetConnectionStringProviderName(String)

傳回 Web.config 檔案之 <connectionStrings> 區段的連接字串提供者。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ParseExpression(String, Type, ExpressionBuilderContext)

傳回物件,表示剖析的運算式。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱