共用方式為


繫結至資料庫

更新:2007 年 11 月

Web 應用程式通常會顯示來自關聯式資料庫 (例如 Microsoft SQL Server、Microsoft Access、Oracle、OLEDB 或 ODBC 資料存放區) 的資料。ASP.NET 提供 LinqDataSourceSqlDataSource 控制項,協助您簡化將控制項繫結至資料庫資料的工作。

LinqDataSource 控制項

LinqDataSource 控制項讓您能在 ASP.NET 網頁中透過宣告式標記,使用 Language-Integrated Query (LINQ) 來擷取和修改資料物件中的資料。此控制項支援自動產生選取、更新、插入和刪除命令。控制項也支援排序、篩選和分頁。

在使用 LinqDataSource 控制項跟資料庫資料進行互動時,請勿將 LinqDataSource 控制項直接連接至資料庫。而是與表示資料庫及表格的實體類別互動。您可以透過物件關聯式設計工具或透過執行 SqlMetal.exe 公用程式,來產生實體類別。如需詳細資訊,請參閱物件關聯式設計工具 (O/R 設計工具)程式碼產生工具 (SqlMetal.exe)

您建立的實體類別通常位於 Web 應用程式的 [App_Code] 資料夾中。O/R 設計工具或 SqlMetal.exe 公用程式會產生一個類別來表示資料庫,並且為資料庫的每一個表格各產生一個類別。

您可以將 ContextTypeName 屬性設定成表示資料庫的類別名稱,將 LinqDataSource 控制項連接到資料庫類別。您可以藉由將 TableName 屬性設定成表示資料的資料表 (Data Table) 的類別名稱,將 LinqDataSource 控制項連接到特定資料表。舉例來說,若要連接到 AdventureWorks 資料庫裡的 Contacts 資料表,可將 ContextTypeName 屬性設定成如 AdventureWorksDataContext 之類的類別名稱 (或您為資料庫物件指定的任何名稱)。將 TableName 屬性設定為 Contacts。

下列範例中的 LinqDataSource 控制項會從名為 Products 的資料表擷取資料。它會自動產生命令,以支援插入、更新與刪除資料。DetailsView 控制項會顯示資料,並建立可讓使用者修改資料的按鈕。

<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    EnableUpdate="true"
    EnableInsert="true"
    EnableDelete="true"
    ID="LinqDataSource1" 
    >
</asp:LinqDataSource>
<asp:DetailsView 
    DataKeyNames="ProductID"
    AutoGenerateEditButton="true"
    AutoGenerateDeleteButton="true"
    AutoGenerateInsertButton="true"
    AllowPaging="true"
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    >
</asp:DetailsView>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    EnableUpdate="true"
    EnableInsert="true"
    EnableDelete="true"
    ID="LinqDataSource1" 
    >
</asp:LinqDataSource>
<asp:DetailsView 
    DataKeyNames="ProductID"
    AutoGenerateEditButton="true"
    AutoGenerateDeleteButton="true"
    AutoGenerateInsertButton="true"
    AllowPaging="true"
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    >
</asp:DetailsView>

如需詳細資訊,請參閱 LinqDataSource Web 伺服器控制項概觀

SqlDataSource 控制項

SqlDataSource 控制項代表 Web 應用程式中資料庫的直接連接。資料繫結控制項 (例如 GridViewDetailsViewFormView 控制項) 能夠使用 SqlDataSource 控制項自動擷取和修改資料。您可以將選取、插入、更新和刪除資料命令指定為 SqlDataSource 控制項的一部分,然後控制項就會自動執行這些作業。您不需要撰寫程式碼 (例如,使用 System.Data 命名空間內之類別的 ADO.NET 程式碼) 建立連接及指定查詢和更新資料庫的命令。

下列程式碼範例示範了繫結至 SqlDataSource 控制項的 GridView 控制項,以擷取、更新和刪除資料。

<%@ 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 >
    <title>GridView Edit Example</title>
</head>
<body>
    <form id="form1" >

      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        >
      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        >
      </asp:sqldatasource>

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

<%@ 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 >
    <title>GridView Edit Example</title>
</head>
<body>
    <form id="form1" >

      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        >
      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        >
      </asp:sqldatasource>

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

SqlDataSource 控制項會直接連接至資料庫,因此實作兩層式資料模型。如果需要繫結至執行資料擷取和更新的中介層商務物件,可以使用 ObjectDataSource 控制項。如需詳細資訊,請參閱繫結至商務物件

如需 SqlDataSource 控制項的詳細資訊,請參閱 SqlDataSource Web 伺服器控制項概觀

繫結至 Microsoft Access 資料庫

ASP.NET 提供 AccessDataSource 控制項以簡化連接至 Microsoft Access 資料庫檔案 (.mdb 檔) 的工作。AccessDataSource 類別繼承自 SqlDataSource 類別,並且使用 System.Data.OleDb .NET Framework 資料提供者和 Microsoft.Jet.OLEDB.4.0 OLE DB 提供者自動連接至 .mdb 檔。若要連接至 Access 資料庫,就要提供檔案路徑做為 DataFile 屬性。除了 AccessDataSource 控制項連接至 Microsoft Access 資料庫的方式不同以外,控制項的運作方式與 SqlDataSource 控制項完全相同。如需詳細資訊,請參閱使用 AccessDataSource Web 伺服器控制項擷取資料

請參閱

概念

ASP.NET 資料存取概觀

LinqDataSource Web 伺服器控制項概觀

SqlDataSource Web 伺服器控制項概觀

使用 AccessDataSource Web 伺服器控制項擷取資料