Microsoft enterprise library 5.0 data access example




















It is common in. The above code casts a double to an int. In the DataObject class, a method DataObject. See the code listing below. This error message is, in fact, quite confusing because a new type of data reader, RefCountingDataReader , shows up while we think we are trying to cast an IDataReader to a SqlDataReader. You would wonder where this reader comes from.

Therefore, we need to look into the RefCountingDataReader to find a way to convert. As a side note, in Enterprise Library version 3. If you are on one of the previous versions of Enterprise Library and plan to upgrade, it might be a good idea to thoroughly test your application under the Enterprise Library 5. On the default. This is to ensure that the database connection is properly managed, because closing the InnerDataReader alone does not release the connection that the outer IDataReader holds.

The above implementation works fine. But we could do it more elegantly. In ASP. NET, the most frequently used method, in my opinion, is:. Yes, we can, utilizing an Extension Method. In our case, it is a really simple static method to write in the namespace DataAccessEntLib , as listed here. The this keyword, plus the parameter of IDataReader in the method, magically makes the method a member of the IDataReader , upon adding the using directive for the namespace of DataAccessEntLib to the default.

The DAABs save you that time. Far too often I see applications that have problems with connection state and connection pooling as well as inconsistencies in the way connection strings are stored. While the connection object may have a simple purpose connect to a data provider , it is often the source of scalability and resource problems.

As shown in Figure 2 , the Enterprise Library DAAB stores connection strings in its own config file, which can be edited using the Enterprise Library Configuration Tool, making editing and maintaining the configuration settings easier than editing an XML configuration file by hand. The DAAB also helps manage connection state for you. For example, if you want to fill a DataSet or insert some rows by executing a stored procedure, the DAAB automatically opens and closes the connection when needed.

It uses the open late, close early approach so that connection pooling is maximized. Of course, you could write this code yourself, but it is easy to forget to close the connection. This code leaves the connection open after the ExecuteReader method executes so the resulting IDataReader object can still access the data:. This returns an IDataReader object. The DAAB keeps the connection open as required. However, if you replaced the last line of code with the following, the DAAB would have opened and closed the connection for you:.

The configuration tool that comes with Enterprise Library provides an easy way to adjust and validate database settings for the DAAB. By doing so, the information can be modified without having to recompile the application. Then navigate to and select the Web. The Configuration Application Block is used to help determine how to store and transform the other Enterprise Library blocks' config files.

This also causes XML to be added to the Web. This file can store database connection string information for multiple connections. The Enterprise Library Config Tool automatically creates entries for connection strings, database instances, and database types.

If you want to use Oracle, however, you can add a new entry here and point it to the Oracle data provider. The dataConfiguration. After you create the new database type by right-clicking on Database Types and selecting New Database Type, you can select the Oracle data provider by clicking on the ellipses on the right side of the TypeName field as shown in Figure 3.

Or, you can add a different data provider by clicking on the Load an Assembly button. The Database Instances node is used to associate one of the database types with one of the connection strings. The name of the database instance is what you use to refer to a particular connection string for a particular provider via code. The default is the database instance that is used by the DatabaseFactory. CreateDatabase method if no argument is passed to it. You can edit the connection string settings under the Connection Strings node.

By default you get a connection string that has a database, server, and integrated security attributes. If you don't want to use integrated security, you can add the User ID and Password connection string settings. When you right-click on the connection string node and choose New Parameter, an attribute is created called Parameter, which you can rename User ID and set its value. You can also add a Password attribute by right-clicking the connection string node and selecting New Password Parameter.

I recommend using integrated security, but if you choose to use SQL Security and pass in the User ID and Password attributes, consider using the Enterprise Library cryptography features to encrypt the connection string.

Database credentials must always be protected. The DAAB stores connection string information in a configuration file, which can then be encrypted. When you choose an encryption algorithm, that algorithm applies to all of the blocks. You can, however, tell each block whether or not it should use the encryption. You can then either create a new key algorithm or choose an existing one.

Let's assume you want to create a new one and you click the Next button. You can then select the algorithm you want to use, and generate the algorithm's key in the next window of the wizard. The following code shows how to call a Stored Procedure that returns a DataSet. The following code shows how to call a Stored Procedure with a parameter that returns a DataTable.

The following code shows a direct statement with a parameter that returns a DataTable. Image 2. View All. Raj Kumar Updated date Feb 16, NET 2.

NET functionality along with the application block's functionality. It reduces the need to write boilerplate code to perform standard tasks. It helps maintain consistent data access practices, both within an application and across the enterprise.



0コメント

  • 1000 / 1000