As the name suggests, the Data Access Application Block is an Application Block responsible for dealing with Data and an important part of the Enterprise Library. I personally think it has a wider application scope and a higher position than other application blocks. (This is just an inappropriate comparison. In fact, each program block is equally important.) unless the application you developed does not access the database, you probably won't be able to use this application block. But remember, it is not the lowest layer of the entire enterprise database. It depends on the Configuration management block, the lowest layer of the Enterprise Library, which will be discussed later. Now I just use the Enterprise Library Configuration tool) to complete the DAAB configuration.
Speaking of these questions, we know that the Enterprise Library evolved from a relatively independent Application Block, in the previous Data Access Application Block, we may build the factory method mode to create database links through static methods. But now that the enterprise database has been released, we also follow the method of getting database connection through configuration management.
First, let's take a look at what DAAB (Data Access Application Block) has brought to us?
1-provides a series of simple and effective methods. The complex data processing logic can be completed through simple parameter transfer, reducing the amount of code.
2-database transparency. For us, it is no longer important for the database to use SQLServer, Oracle, or DB2. Data access differences in various databases are encapsulated by providing consistent access models.
3 -- the best ADO. NET practice. Because it is developed by a dedicated Microsoft Development Team, this data access model is time-proven, the most complete, efficient, and authoritative, and adopts ADO. NET to operate databases.
4-provides a consistent and robust data access model for team development.
...... And so on.
Next, we will give you a simple experience on how to use DAAB:
(Because there is no time relationship, please forgive me)
In summary, using DAAB can be abstracted into three steps: Configuring DAAB, instantiating the database, and performing specific data operations. The description will be expanded later.
First, create configuration information. As mentioned above, the enterprise database architecture creates database connections through configuration. We can use the Enterprise Library Configuration tool to quickly and securely complete the Configuration. The procedure is as follows:
Preparation: This step is very important. We have created a new WinForm project, which requires an additional item (application configuration file ). In this way, you can directly use the configuration tool to open the configuration file for editing, saving you trouble.
1. Open the Configuration tool. (I use the XP system, and the Enterprise Library version is Enterprise Library-June 2005) Start Menu-> All Programs-> Microsoft patterns & practices-> Enterprise Library-June 2005-> Enterprise Library Configuration
2. Open the application. File-> Open Application: select the App. config or Web. config File in the project directory.
3. Create a DAAB. Right-click the newly created Application and choose New> Data Access Application Block.
4. Configure the DAAB parameter. There is too much content here. Let's just talk about several common configuration items. First, the Database instance name. Under the "Database Instances" node, the Instance name "Database Instance1" is created by default. This name is used to create a Database instance in the program, please change it to a distinguished name. The second is the Connection string. A Connection string will be created by default under the "Connection Strings" node. We can also change the name. The default value of "Integrated Security" in the connection string is True, which indicates that Windows Integrated authentication is used (user name and password are not required). If we use a hybrid authentication method, set the value to False first, then add the username and password attributes in the Connection String, and fill in the correct attribute values (method: right-click a Connection String node and choose New from the context menu ). Everything is ready to be saved.
The second step is to create a database instance.
Preparation: You need to reference the enterprise database to the project. Because the Enterprise Library is open-source, we can directly reference the project or reference the compiled DLL. To illustrate how to use the tool, only two key assemblies, Microsoft. Practices. EnterpriseLibrary. Configuration. dll and Microsoft. Practices. EnterpriseLibrary. Data. dll, are referenced here. In addition, to simplify code writing, we can use the Microsoft. Practices. EnterpriseLibrary. Data namespace where DAAB is needed.
After a long time, I finally started to enter the climax. Hey! Before performing database operations, you must create a database instance. Use the following statement:
Database db = DatabaseFactory. CreateDatabase ("Northwind ");
This statement completes the database connection operation. It's easy enough! Note that the Northwind here is the name of the database instance described in the previous configuration, rather than the name of the database to be connected. The database name is defined together with the server name, user name, and password in the ConnectionStringNode attribute of the database instance. Please clarify this relationship.
In addition, Database instances maintain Database connections during the lifecycle. That is, when a database instance is produced from a factory, ADO is closed internally. NET Connection. When the lifecycle ends (assumed in a pair of curly brackets), it will automatically call the close method to close the Connection.
Part 3: specific database operations.
DAAB provides us with a powerful, secure, and efficient method, but it is because of time issues that cannot be elaborated on in every detail. Here we only use the most direct and simple method to demonstrate its role, I hope you will have time to discuss with us about various DAAB database operations in the future. Here we will perform the following operations to obtain a dataset by executing an SQL statement:
DataSet ds = db. ExecuteDataSet (CommandType. Text, "select * from Employees ");
In the same way, it is also a sentence. After the database is complete, you can leave it to yourself.
We can see that after using DAAB, data access has never been simpler. However, DAAB and the entire enterprise library are far more embedded and valuable than that, but I believe that your love for them will gradually deepen with your understanding. Today we are talking about it. It's so embarrassing. Good night!