Ligando a bancos de dados
Aplicações Web comumente exibem dados obtidos de bancos de dados relacionais como o Microsoft SQL Server, Microsoft Acces, Oracle, ou um repositório de dados OLEDB ou ODBC.To simplify the task of binding a control to data from a database, ASP.NET provides the LinqDataSource and SqlDataSource controls.
The LinqDataSource Control
The LinqDataSource control enables you to use Language-Integrated Query (LINQ) in an ASP.NET Web page through declarative markup in order to retrieve and modify data from a data object.The control supports automatic generation of select, update, insert, and delete commands.The control also supports sorting, filtering, and paging.
When you use the LinqDataSource control to interact with data from a database, you do not connect the LinqDataSource control directly to the database.Instead, you interact with entity classes that represent the database and the tables.You can generate the entity classes through the Object Relational Designer or by running the SqlMetal.exe utility.Para obter mais informações, consulte Object Relational Designer (O/R Designer) e Ferramenta de geração de código (SqlMetal.exe).
The entity classes that you create will typically be located in the App_Code folder of the Web application.The Designer Relacional de Objetos or the SqlMetal.exe utility will generate one class that represents the database and one class for each table in the database.
You connect the LinqDataSource control to a database class by setting the ContextTypeName property to the name of the class that represents the database.You connect the LinqDataSource control to a particular table by setting the TableName property to the name of the class that represents the data table.For example, to connect to the Contacts table in the AdventureWorks database, you set the ContextTypeName property to a class name such as AdventureWorksDataContext (or whatever name that you specify for the database object).You set the TableName property to Contacts.
The following example shows a LinqDataSource control that retrieves data from a table named Products.It automatically generates the commands to support inserting, updating, and deleting data.A DetailsView control displays the data and creates buttons that let users modify the data.
<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>
Para obter mais informações, consulte Visão geral sobre o controle de servidor Web LinqDataSource.
The SqlDataSource Control
The SqlDataSource control represents a direct connection to a database in a Web application.Data-bound controls such as the GridView, DetailsView, and FormView controls can use the SqlDataSource control to retrieve and modify data automatically.You specify commands to select, insert, update, and delete data as part of the SqlDataSource control, and the control performs these operations automatically.You do not need to write code (for example, ADO.NET code that uses classes in the System.Data namespace) to create a connection and specify commands to query and update a database.
The following code example shows a GridView control bound to a SqlDataSource control to retrieve, update, and delete data.
<%@ 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>
The SqlDataSource control directly connects to a database and therefore implements a two-tiered data model.If you need to bind to a middle-tier business object which performs data retrieval and updates, you can use the ObjectDataSource control.Para obter detalhes, consulte:Vinculando a objetos corporativos.
For more information about the SqlDataSource control, see the Visão geral sobre o controle de servidor Web SqlDataSource.
Binding to a Microsoft Access Database
ASP.NET provides an AccessDataSource control that simplifies the task of connecting to a Microsoft Access database file (.mdb file).The AccessDataSource class inherits from the SqlDataSource class and automatically connects to an .mdb file using the System.Data.OleDb .NET Framework data provider and the Microsoft.Jet.OLEDB.4.0 OLE DB provider.To connect to an Access database, you supply a file path as the DataFile property.Other than the difference in how the AccessDataSource control connects to a Microsoft Access database, the control works exactly like the SqlDataSource control.Para obter mais informações, consulte Recuperando dados usando o controle de servidor Web AccessDataSource.
Consulte também
Conceitos
Visão Geral do Acesso a Dados ASP.NET
Visão geral sobre o controle de servidor Web LinqDataSource
Visão geral sobre o controle de servidor Web SqlDataSource
Recuperando dados usando o controle de servidor Web AccessDataSource