This document is maintained at: http://wiki.entlib.net.cn/entlibhelp31dataaccessapplicationblock.ashx.
If you have any questions, log on to the. NET Enterprise Database Technology Forum (http://forum.entlib.net.cn ).
Enterprise Library
Data Access ApplicationProgramBlock simplifies the development tasks for implementing the regular data access function. Applications can use this Application Block in various scenarios, such as reading data for display and passing data through the application layer (
Application layers), and submit the modified data back to the database system. The application block contains stored procedures and inline SQL
. General internal processing (housekeep), such as managing connections, creating and caching parameters, are encapsulated in the Application Block Method. In other words, data access application blocks are in easy-to-use classes.
Provides access to the most common features of ADO. Net, which improves the efficiency of developers.
Ado. NET 2.0 providesDbcommandClass andDbconnection
Such classes help abstract data providers from any specific database implementation. Data access application blocks take advantage of these classes and provide models that enhance support for specific database feature encapsulation, such
Parameter discovery and type conversion. Therefore, applications canCodeIn this case, you can port data from one database to another. The Data Access Application Block contains an abstract base class, which defines
And provides many data access methods available in ADO. NET 2.0.
The Application Block also contains
Microsfot SQL Server, Microsoft SQL Server ce, and Microsoft el
. These classes perform operations on specific database types. The application code is only written for one type of database, such as SQL
Server, you can see that many of the code written for another database is the same, such as oracle.
Another feature of the Data Access Application Block is that the application code can be directed to a specific data by the name of An ADO. net connection string, such as "customer" or "inventory. The application code can specify a database named instance and pass this parameterDatabasefactory. createdatabaseMethod. Each named database has a connection string stored in the configuration file. By modifying the settings in the configuration file, developers can use applications in different database configurations without re-compiling the code.
Data access application blocks provide the following benefits:
- Using and using the functions provided by ADO. NET 2.0, you can use both ADO. NET and Application Block functions.
- Reduce the need to write duplicate codes to complete standard tasks.
- This helps maintain consistent data access practices, both within and between applications.
- Reduces the difficulty of changing the database type.
- Free developers from learning different programming models for different databases.
- Reduces the amount of code that developers have to write when porting applications to another database.
Common scenarios
Open
Developers often write database applications. Because it is so common, developers may find that they repeatedly write the same code for each application. In addition, these applications may need different
Databases work together. Although the tasks are the same, the Code must be adapted to adapt to the programming model of each database. The Data Access Application Block provides the logic to complete the most common data access tasks.
Question. Developers only need to do the following:
- Create a database object.
- Provides command parameters, if needed.
- Call appropriate methods. These methods have been optimized for performance and can be transplanted.
The Data Access Application Block can work transparently with SQL Server, SQL Server ce, and Oracle databases.
Sample application code
The following code calls a stored procedure and returnsDataset.
C #
Database DB = databasefactory. createdatabase ();
Dbcommand = dB. getstoredproccommand ("getproductsbycategory ");
// Retrieve products from category 7.
DB. addinparameter (dbcommand, "categoryid", dbtype. int32, 7 );
Dataset productdataset = dB. executedataset (dbcommand );
Visual Basic
Dim dB as database = databasefactory. createdatabase ()
Dim dbcommand as dbcommand = dB. getstoredproccommand ("getproductsbycategory ")
'Retrieve products from the category 7.
DB. addinparameter (dbcommand, "categoryid", dbtype. int32, 7)
Dim productdataset as dataset = dB. executedataset (dbcommand)
Reader requirements
The target readers of this manual are software architects and software developers. To maximize the benefits from this manual, you will need to understand the following technologies:
- Microsoft Visual Studio 2005 Development System (Any of the following versions ):
- Visual Studio 2005 Professional Edition
- Visual Studio 2005 team system Edition
- Visual Studio 2005 Standard Edition
- . NET Framework Version 2.0
- Microsoft SQL Server, Microsoft SQL Server ce, or Oracle Database
Migrate to Enterprise Library 3.1-May 2007
Generally, applications built using January 2006 of the Application Block for data access can use the features released by May 2007 without having to modify any code. You may need to update the reference to point to the new assembly and update the configuration file to reference the correct application version.
Methods of some database classes have been modified to use. NET Framework 2.0TransactionscopeClass. These methods, suchExecutenonquery, UsedGetopenconnectionMethod replacedGetconnectionMethod To be modified to recognizeTransactionscopThe effective time of the instance. If you writeDatabaseClass, you need to consider these changes to rewrite the code. If you continue to useGetconnectionMethod, you will receive a compilation error.
In addition, if the application usesExecutexmlreaderMethod, you may need to rewrite the code to test and view before closing the connectionTransactionscopeWhether the instance is valid.
For more information, see use the transactionscope class and how to useExecutexmlreaderThe XML example of the method to obtain multiple rows of data.
New Features
Enterprise
Library 3.1-May 2007 supports SQL Server compact edition (CE ). SQL Server CE
Provides a refined relational database feature for desktop and mobile applications. These applications require local database storage but do not require a complete SQL Server
. For more information, see use SQL Server ce.
Database. updatedatasetMethod has a new useUpdatebatchsizeParameter overload. SetUpdatebatchsizeIf the parameter is a positive integerDataadapterObjects are sent to the database in batches with the specified size. This will reduce the number of database round-trips.
System Requirements
To run data access application blocks, you need the following systems:
- Microsoft Windows XP Professional, Windows Server 2003, or Windows Vista
- Microsoft. NET Framework 2.0
- Microsoft Visual Studio 2005 Development System (Any of the following versions ):
- Microsoft Visual maxcompute 2005 Standard Edition
- Microsoft Visual maxcompute 2005 Professional Edition
- Microsoft Visual Studio 2005 team edition for software developers
- Microsoft Visual Studio 2005 team edition for Software testers
- Microsoft Visual Studio 2005 team edition for software release TS
- Microsoft Visual Studio 2005 team suite
- Operation
The database server that is supported by the. NET Framework 2.0 data provider. Includes SQL Server 2000 or an updated version.
Server ce and Oracle 9i or later. The running server can also run. net
The database supported by the Framework 2.0 data provider.
Data Access Application Block dependency
The Data Access Application Block depends on other code in the Enterprise Library:
- The function of the kernel library.The Enterprise Library Kernel provides services such as measurement and configuration, which are shared dependencies of all enterprise library application blocks. The kernel library function is included in the AssemblyMicrosoft. Practices. enterpriselibrary. Common. dll.
- ObjectbuilderSubsystem.ObjectbuilderThe subsystem completes all repeated and necessary tasks for destroying object instances, while providing high flexibility. Enterprise LibraryObjectbuilderSubsystems are used for tasks such as injecting a measurement class into a program block class and connecting a measurement class to an application block.ObjectbuilderSubsystem included in assemblyMicrosoft. Practices. objectbuilder. dll.
The Data Access Application Block uses ADO. Net to manage connection strings. The Application Block allows you to use configuration information to create a database object.DatabasefactoryClass. The configuration information is the connection string and other and program block configuration information managed by ADO. net.
Note: : By default, the configuration information source of the Application Block is the configuration file of the application. You can use the Enterprise Library to modify the configuration information source. For example, you can save the configuration settings of the application block to the SQL database.
Database The factory class first tries to read the connection string information from the default configuration source. If no,
Database The factory class then tries to read the connection string information from the ADO. Net Management Configuration section in the application configuration file.
Generally, you can use configuration data to manage connection strings. Another is to create a database object in the application by using the build function and providing it with the connection string and database type.
The recommended method for modifying the configuration information of data access application blocks is to use the Enterprise Library configuration console.
Documents of data access application blocks
In addition to the introduction, the document also includes the following parts:
- Use Data to access application blocks to develop applications. This section is divided into several subchapters. Enter the configuration information to describe how to configure the Application Block. The added application code describes how
Use the Data Access Application Block to prepare the application. The next section describes how to use an application block to complete most typical data access operations. The last section describes the development task details, including
Depth Information about creating database objects, creating commands, processing parameters, and handling exceptions.
- The Design of Data Access application blocks. This section describes the design of application blocks and related decisions.
- Extends and modifies Data Access application blocks. This section describes how to add your own database provider to expand the application and provide modifications.Source code.
- Deployment and operations. This section describes how to deploy and update the application block assembly, as well as configuration and SQL server security information.
- Quick Start. This section describes how to install and configure the Quick Start application. It also describes how to add common data access operations to roaming applications.
More information
For more information, see the following resources:
- Application Architecture for. Net: designing applications and services
- . NET data access architecture Guide
- Info: Microsoft guide for designing data tier components and passing data through tiers
- Improving Web Application Security: Threats and Countermeasures
- Improving. NET application performance and scalability
1.1-scenarios and objectives
The Data Access Application Block is designed to solve the vast majority of common tasks that developers face when writing database applications. These tasks are organized according to the scenario. A real-world example is provided for each scenario. For example, you can obtain information from a category or complete a bank transaction, describe the database functions required by the condition, and display the code for completing the task.
The purpose of organizing these tasks according to the scenario is to give the code context to demonstrate a group of isolated methods instead of where they are best used, the scenario provides a setting for the code to place it in the conditions that many developers are familiar with when their applications must access the database.
The scenario is as follows:
- UseDatareaderObtain multi-row data
- UseDatasetObtain multi-row data
- Run a command and obtain the output parameters.
- Run a command to obtain a single value
- Execute multiple operations in a transaction
- Obtain XML data from SQL Server
- Update a database with data contained in the DataSet object
When to use data to access application blocks
The Data Access Application Block contains a few methods to simplify the common methods for accessing the vast majority of databases. Each method encapsulates the logic required to obtain data and manage database connections. If the application uses standard data access technology, you can consider using application blocks.
The Application Block supplements the code in ADO. NET 2.0 so that you can use the same code in different database types. It contains classes for SQL Server and Oracle databases. These classes contain code that provides specific database features such as parameter processing and cursor implementation. In addition,GenericdatabaseClass allows the use of application blocks and any configuration of ADO. NET 2.0DbproviderfactoryObject. You can extend the application block by adding new database-specific features or providing database types customized for existing databases. It only needs to be in the ADO. NET 2.0 for the target database.DbproviderfactoryClass.
When to use ADO. Net directly
Quantity
The Data Access Application Block is a supplement to ADO. net, rather than a replacement. Application blocks simplify and help developers use ADO. Net with best practices.
. If the application needs to obtain data in a special way, or the code needs to be customized to take advantage of the features specific to the specific database, the use of ADO. NET may be more suitable.