ADO.NET 架構ADO.NET Architecture

傳統的資料處理主要是依賴相互連接的雙層式模型。Data processing has traditionally relied primarily on a connection-based, two-tier model. 隨著資料處理朝多層式架構發展,程式設計人員也逐漸改用中斷連接的方式,使應用程式更具延展性 (Scalability)。As data processing increasingly uses multi-tier architectures, programmers are switching to a disconnected approach to provide better scalability for their applications.

ADO.NET 元件ADO.NET Components

ADO.NET 3.0ADO.NET 3.0 中用於存取和管理資料的兩個主要元件是 .NET Framework.NET Framework 資料提供者和 DataSetThe two main components of ADO.NET 3.0ADO.NET 3.0 for accessing and manipulating data are the .NET Framework.NET Framework data providers and the DataSet.

.NET Framework 資料提供者.NET Framework Data Providers

.NET Framework 資料提供者是一種明確設計用於管理資料以及快速存取順向唯讀資料的元件。The .NET Framework Data Providers are components that have been explicitly designed for data manipulation and fast, forward-only, read-only access to data. Connection 物件會提供資料來源的連接。The Connection object provides connectivity to a data source. Command 物件可讓您存取資料庫命令,以便傳回資料、修改資料、執行預存程序 (Stored Procedure),並且傳送或擷取參數資訊。The Command object enables access to database commands to return data, modify data, run stored procedures, and send or retrieve parameter information. DataReader 則可提供來自資料來源的高效能資料流。The DataReader provides a high-performance stream of data from the data source. 最後,DataAdapter 會提供 DataSet 物件與資料來源之間的橋接器 (Bridge)。Finally, the DataAdapter provides the bridge between the DataSet object and the data source. DataAdapter 會使用 Command 物件於資料來源處執行 SQL 命令,以便將資料載入 DataSet,並且將 DataSet 內的資料變更調節回資料來源。The DataAdapter uses Command objects to execute SQL commands at the data source to both load the DataSet with data and reconcile changes that were made to the data in the DataSet back to the data source. 如需詳細資訊,請參閱 < .NET Framework 資料提供者擷取和修改 ADO.NET 中的資料For more information, see .NET Framework Data Providers and Retrieving and Modifying Data in ADO.NET.

DataSetThe DataSet

ADO.NET DataSet 的設計已明確指出它可獨立於任何資料來源外而存取資料。The ADO.NET DataSet is explicitly designed for data access independent of any data source. 因此,它可與多個不同的資料來源搭配使用、與 XML 資料搭配使用,或用來管理應用程式的本機資料。As a result, it can be used with multiple and differing data sources, used with XML data, or used to manage data local to the application. DataSet 包含一或多個由資料列和資料行所組成的 DataTable 物件集合,以及 DataTable 物件中的主索引鍵、外部索引鍵、條件約束 (Constraint) 及資料的關聯資訊。The DataSet contains a collection of one or more DataTable objects consisting of rows and columns of data, and also primary key, foreign key, constraint, and relation information about the data in the DataTable objects. 如需詳細資訊,請參閱 < Dataset、 Datatable 和 DataviewFor more information, see DataSets, DataTables, and DataViews.

下圖將說明 .NET Framework.NET Framework 資料提供者與 DataSet 之間的關聯性。The following diagram illustrates the relationship between a .NET Framework.NET Framework data provider and a DataSet.

ADO.Net 圖形ADO.Net graphic
ADO.NET 架構ADO.NET architecture

選擇 DataReader 或 DataSetChoosing a DataReader or a DataSet

當您決定是否應該使用您的應用程式DataReader(請參閱擷取的資料使用 DataReader) 或DataSet(請參閱Dataset、 Datatable 和 Dataview),請考慮的型別應用程式所需的功能。When you decide whether your application should use a DataReader (see Retrieving Data Using a DataReader) or a DataSet (see DataSets, DataTables, and DataViews), consider the type of functionality that your application requires. 請使用 DataSet 來進行下列作業:Use a DataSet to do the following:

  • 請快取應用程式本機的資料,如此您才能夠管理它。Cache data locally in your application so that you can manipulate it. 若您只需要讀取查詢結果,DataReader 將是比較理想的選擇。If you only need to read the results of a query, the DataReader is the better choice.

  • 在各層間或從 XML Web Service 遠端處理資料。Remote data between tiers or from an XML Web service.

  • 與資料動態互動 (例如繫結 Windows Form 控制項),或是將來自多個來源的資料合併和關聯。Interact with data dynamically such as binding to a Windows Forms control or combining and relating data from multiple sources.

  • 針對資料進行廣泛處理,並不需要與資料來源間有開放連接,如此便可將連接釋放給其他用戶端使用。Perform extensive processing on data without requiring an open connection to the data source, which frees the connection to be used by other clients.

如果您不需要 DataSet 所提供的功能,則可採用 DataReader 以順向、唯讀的方式來傳回資料,藉以提升應用程式的效能。If you do not require the functionality provided by the DataSet, you can improve the performance of your application by using the DataReader to return your data in a forward-only, read-only manner. 雖然DataAdapter會使用DataReader來填滿的內容DataSet(請參閱從 DataAdapter 填入 DataSet),使用DataReader,可以提升效能,因為您將節省記憶體會由DataSet,並避免所需建立和填入的內容處理DataSetAlthough the DataAdapter uses the DataReader to fill the contents of a DataSet (see Populating a DataSet from a DataAdapter), by using the DataReader, you can boost performance because you will save memory that would be consumed by the DataSet, and avoid the processing that is required to create and fill the contents of the DataSet.

LINQ to DataSetLINQ to DataSet

LINQ to DataSet 會針對在 DataSet 物件中快取的資料提供查詢功能和編譯階段型別檢查。LINQ to DataSet provides query capabilities and compile-time type checking over data cached in a DataSet object. 它可讓您使用其中一種 .NET Framework 開發語言 (例如 C# 或 Visual Basic) 來撰寫查詢。It allows you to write queries in one of the .NET Framework development language, such as C# or Visual Basic. 如需詳細資訊,請參閱 LINQ to DataSetFor more information, see LINQ to DataSet.


LINQ to SQL 可針對對應到關聯式資料庫資料結構的物件模型進行查詢,而不必使用中繼的概念模型。LINQ to SQL supports queries against an object model that is mapped to the data structures of a relational database without using an intermediate conceptual model. 每個資料表都是由個別的類別表示,將物件模型與關聯式資料庫結構描述緊密結合。Each table is represented by a separate class, tightly coupling the object model to the relational database schema. LINQ to SQL 會將物件模型中的 Language-Integrated Query 轉譯成 Transact-SQL,然後將其傳送至資料庫進行執行。LINQ to SQL translates language-integrated queries in the object model into Transact-SQL and sends them to the database for execution. 當資料庫傳回結果時,LINQ to SQL 會將結果轉譯回物件。When the database returns the results, LINQ to SQL translates the results back into objects. 如需詳細資訊,請參閱 LINQ to SQLFor more information, see LINQ to SQL.

ADO.NET Entity FrameworkADO.NET Entity Framework

ADO.NET Entity Framework 的設計目標是要讓開發人員針對概念應用程式模型進行程式設計來建立資料存取應用程式,而不用直接對關聯式儲存結構描述進行程式設計。The ADO.NET Entity Framework is designed to enable developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. 其目的是要減少資料導向應用程式所需程式碼和維護的工作量。The goal is to decrease the amount of code and maintenance required for data-oriented applications. 如需詳細資訊,請參閱 < ADO.NET Entity FrameworkFor more information, see ADO.NET Entity Framework.

WCF 資料服務WCF Data Services

WCF 資料服務WCF Data Services 是用於在 Web 或內部網路上部署資料服務。 is used to deploy data services on the Web or an intranet. 系統會根據實體資料模型的規格,讓資料結構化成實體與關聯性。The data is structured as entities and relationships according to the specifications of the Entity Data Model. 部署在這個模型上的資料可由標準 HTTP 通訊協定定址。Data deployed on this model is addressable by standard HTTP protocol. 如需詳細資訊,請參閱 < WCF Data Services 4.5For more information, see WCF Data Services 4.5.


ADO.NETADO.NET 會利用 XML 的功能,以中斷連接的方式存取資料。 leverages the power of XML to provide disconnected access to data. ADO.NETADO.NET 中,已將 .NET Framework.NET Framework 和 XML 類別設計得相當緊密,因為這兩者都是同一個架構上的元件。ADO.NETADO.NET was designed hand-in-hand with the XML classes in the .NET Framework.NET Framework; both are components of a single architecture.

ADO.NETADO.NET 中的 .NET Framework.NET Framework 和 XML 類別的交集在於 DataSet 物件。 and the XML classes in the .NET Framework.NET Framework converge in the DataSet object. DataSet 可以填入 XML 來源的資料,無論它是檔案或 XML 資料流都一樣。The DataSet can be populated with data from an XML source, whether it is a file or an XML stream. 不管 DataSet 的資料來源為何,DataSet 都可組合成與全球資訊網協會 (W3C) 相容的 XML,而且包含其結構描述當做 XML 結構描述定義語言 (XSD) 結構描述。The DataSet can be written as World-Wide Web Consortium (W3C) compliant XML that includes its schema as XML schema definition language (XSD) schema, regardless of the source of the data in the DataSet. 由於 DataSet 的原生序列化格式是 XML,所以相當適合當做在各層之間移動資料的媒體,如此當需要在遠端對 XML Web Service 來回傳送資料和結構描述內容時,DataSet 將會是最佳選擇。Because of the native serialization format of the DataSet is XML, it is an excellent medium for moving data between tiers, making the DataSet an optimal choice for remoting data and schema context to and from an XML Web service. 如需詳細資訊,請參閱 XML 文件和資料For more information, see XML Documents and Data.

另請參閱See Also

ADO.NET 概觀ADO.NET Overview
ADO.NET Managed 提供者和 DataSet 開發人員中心ADO.NET Managed Providers and DataSet Developer Center