Using Enterprise Library and DB2

One of the things I do (beyond my work in PLM) is to help customers figure out how to use .NET and our suite of development tools to solve the business problems they encounter on a daily basis.  Presently, I'm at work on a rather long term engagement at one of the big three here in Detroit and we've had the need to help them figure out how to use the Enterprise Library download inconjunction with their database platform standard of DB2.

So, in helping them with this I found out that there really wasn't a great deal of information out there to really point a developer at to help them figure out how one should go about doing this.  It is in the spirit of helping those who find themselves in a similar situation that I make this post.

In the beginning there was a build

The first step in beginning to use Enterprise Library with DB2 is to reconginze that you will have to rebuild the enterprise library in order to have DB2 available as one of the options to you when using the Data Access block.  This can be done by first ensuring that you have access to the managed provider library provided by IBM.  Once you have determined that you have this open up the Enterprise Library solution within Visual Studio and add the DB2 project to the solution (this can be found in the src\Data\DB2 directory within your Enterprise Library installation directory).  Then, make sure that you've added the project to the overall Enterprise Library solution you'll also need to make sure that you've added the managed provider references to the set of project references required by the DB2 Data Access project.

Now, since you've got the solution/project structure established correctly (that is with the DB2 library present in the solution) go ahead and run the buildLibrary batch file (this can be found in the \src directory within your Enterprise Library installation directory).  Provided this completes without error then go ahead and run the CopyAssemblies.bat script (NOTE: you may want to create a backup version prior to executing this batch file just to cover yourself in case of error).  This batch file will replace the assemblies which are placed in the \bin directory as part of the normal installation process.

Getting up and running

So, now that the assemblies have been built and your newly built assemblies have been copied over to the bin directory you can go ahead and run the configuration tool.  If you create your application (File->New Application) and add a data access block (Action->Data Access Application Block) you will see the normal configuration options present.  Since we want to replace the DB type here from SQLServer to DB2 you can highlight the Database Types node in the tree and right click (here you will select the New->Database Type entry).

Now, this will present a new panel which will give you two entries (general and typename).  If you click on the button with the (...) next to the typename textfield you will see a dialog presented which will allow you to select the Database provider to use.  One of these should be, "DB2Database".  Select the DB2Database and press OK (and optionally change the name associated with the Database Type).

It's all about connections

The next step is to modify the connection string.  This, of course, varies from vendor to vendor so you will almost certainly want to get rid of the, "Integrated Security" member of the connection string as this isn't exactly valid on DB2.  Listed below is an example (a very simple and minimal example) of my dataConfiguration.config that was produced from my run of the configuration tool.  Hopefully, this will give you some insight on what options to add or remove from this portion of the connect string.

<?xml version="1.0" encoding="utf-8"?>

<dataConfiguration>

<xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">

<enterpriseLibrary.databaseSettings xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" defaultInstance="testdb" xmlns="https://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">

<databaseTypes>

<databaseType name="DB2" type="Microsoft.Practices.EnterpriseLibrary.Data.DB2.DB2Database, Microsoft.Practices.EnterpriseLibrary.Data.DB2, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />

</databaseTypes>

<instances>

<instance name="TESTDB" type="DB2" connectionString="DB2ConnectionString" />

</instances>

<connectionStrings>

<connectionString name="DB2ConnectionString">

<parameters>

<parameter name="database" value="TESTDB" isSensitive="true" />

<parameter name="user Id" value="db2_user_name" isSensitive="true" />

<parameter name="password" value="db2_password" isSensitive="true" />

<parameter name="server" value="localhost" isSensitive="false" />

</parameters>

</connectionString>

</connectionStrings>

</enterpriseLibrary.databaseSettings>

</xmlSerializerSection>

</dataConfiguration>

Hopefully, you find this helpful to you. If you need drop me a comment.