绑定到数据库

更新:2007 年 11 月

Web 应用程序通常显示来自关系数据库(如 Microsoft SQL Server、Microsoft Access、Oracle、OLEDB 或 ODBC 数据存储区)的数据。为了简化将控件绑定到数据库中的数据这一任务,ASP.NET 提供了 LinqDataSourceSqlDataSource 控件。

LinqDataSource 控件

利用 LinqDataSource 控件,可通过声明性标记在 ASP.NET 网页中使用语言集成查询 (LINQ),以检索和修改数据对象中的数据。该控件支持自动生成选择、更新、插入和删除命令。该控件还支持排序、筛选和分页。

在使用 LinqDataSource 控件与数据库中的数据进行交互时,LinqDataSource 控件并不直接连接至数据库。而是与表示数据库和表的实体类进行交互。通过 对象关系设计器或运行 SqlMetal.exe 实用工具可生成实体类。有关更多信息,请参见对象关系设计器(O/R 设计器)代码生成工具 (SqlMetal.exe)

通常,创建的实体类将位于 Web 应用程序的 App_Code 文件夹中。O/R 设计器或 SqlMetal.exe 实用工具将生成一个表示数据库的类,并为该数据库中的每个表生成一个类。

通过将 ContextTypeName 属性设置为表示数据库的类的名称,将 LinqDataSource 控件连接到数据库类。通过将 TableName 属性设置为代表数据表的类的名称,将 LinqDataSource 控件连接到特定表。例如,若要连接到 AdventureWorks 数据库中的 Contacts 表,可将 ContextTypeName 属性设置为类似于 AdventureWorksDataContext 的类名(或您为数据库对象指定的任何其他名称)。将 TableName 属性设置为 Contacts。

下面的示例演示了一个从名为 Products 的表中检索数据的 LinqDataSource 控件。该控件可自动生成支持插入、更新和删除数据的命令。DetailsView 控件用于显示数据并创建供用户修改数据的按钮。

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

有关更多信息,请参见 LinqDataSource Web 服务器控件概述

SqlDataSource 控件

SqlDataSource 控件表示到 Web 应用程序中数据库的直接连接。数据绑定控件(如 GridViewDetailsViewFormView 控件)可以使用 SqlDataSource 控件自动检索和修改数据。可以将用来选择、插入、更新和删除数据的命令指定为 SqlDataSource 控件的一部分,并让该控件自动执行这些操作。您无需编写代码(例如,使用 System.Data 命名空间中的类的 ADO.NET 代码)来创建连接并指定用于查询和更新数据库的命令。

下面的代码示例演示如何将 GridView 控件绑定到 SqlDataSource 控件以检索、更新和删除数据。

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

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

      <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"  
        runat="server">
      </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%>"
        runat="server">
      </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 服务器控件检索数据