IBM DB2 issue--System.BadImageFormatException: Cou

2019-04-11 09:48发布

I am getting the above mentioned error message. I have made enough research and tried out various options but still no luck.

Here arethe details below:

OS: Windows 7 64 bit version

    Visual studio version: 2013 Premium

   .NET framework version is : 4.0.30319

   ASP.NET version: 4.0.30319.34249

   IIS version: 7.0

   DB2 installed path: C:\Program Files (x86)\IBM

   DLL path: C:\Program Files (x86)\IBM\SQLLIB\BIN\netf40\IBM.DB2.dll

   DLL Version: 9.7.4.4

I have also changed my solution configuration manager platform to Any CPU and also I have changed the settings in the application pool by setting the property enable 32-bit application to True.

But still am getting the same error. Is there any other workaround?

Please help me with your suggestions.

6条回答
闹够了就滚
2楼-- · 2019-04-11 10:27

DB2 Install

  • Download latest IBM Data Server Driver Package from this (https://www-01.ibm.com/support/docview.wss?uid=swg27016878) link.
  • Execute the file As Administrator and keep defaults except for the place where it asks to install features on your local driver. Click on that and select Install all features and subfeatures ... on your local drive. Not sure if it effects.
  • The driver will be installed by default in C:\Program Files\IBM\IBM DATA SERVER DRIVER.
  • Instance will be installed by default in C:\ProgramData\IBM\DB2\IBMDBCL1. Default instance name for this is IBMDBCL1.
  • You should set your connetion sting in this location C:\ProgramData\IBM\DB2\IBMDBCL1\cfg, in this file db2dsdriver.cfg.
 <dsncollection>       
  <dsn alias="DBDSNSAMPLE" name="DBDSNSAMPLE" host="127.0.0.1" port="50001"/>          
 </dsncollection>     
 <databases>       
  <database name="DBDSNSAMPLE" host="127.0.0.1" port="50001">                
  </database>          
 </databases> 
</configuration>
  • Note: you can uninstall the driver from Programs and Features
  • After adding connection you can test the connection with testconn40.exe. Open cmd and locate to C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin and execute:

    > testconn40 DBDSNSAMPLE

  • This command won't login you (because we did't specify user credentials in .cfg file), but at least it will show the proper message, which means your .NET app will work. Otherwise, it will show some exception errors.

DB2 in Visual Studio

  • Install IBM.Data.DB2 package from nuget.
  • Then, in the References you will see both IBM.Data.DB2.dll and IBM.Data.DB2.Entity.dll.
  • Here is the testing code:
   using (var conn = new DB2Connection($"Database={u.db2a};UserID={u.db2u};Password={u.db2p};Server={u.db2i}"))
   using (var cmd = new DB2Command())
   {

       cmd.CommandText = "SELECT * FROM Table1";
       cmd.Connection = conn;
       conn.Open();
       using (var reader = cmd.ExecuteReader())
       {
           while (reader.Read())
           {

               Console.WriteLine(reader.GetValue(3));
           }
       }
   }

Notes

  • In case if dlls are having issues: Go to References and click on IBM.Data.DB2, in Properies windows set Copy Local to False. Same thing to IBM.Data.DB2.Entity . (This will make sure that those two dlls will not show up in bin folder anytime, when you build/compile your project. Instead project will use these DLLS from GAC. And we are using them only to resolve our code)
  • Another thing is, it might be that those dlls were not install correctly in GAC while initial installation. Thus you may try to install them manually: Go to Developer Command Prompt for Visual Studio and run those lines step by step:
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf20\IBM.Data.DB2.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf20_32\IBM.Data.DB2.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf40\IBM.Data.DB2.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf40_32\IBM.Data.DB2.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf20\IBM.Data.Informix.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf20_32\IBM.Data.Informix.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf40\IBM.Data.Informix.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf40_32\IBM.Data.Informix.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf20\IBM.Data.DB2.Entity.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf20_32\IBM.Data.DB2.Entity.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf40\IBM.Data.DB2.Entity.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\netf40_32\IBM.Data.DB2.Entity.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\db2app.dll"
gacutil /i "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\db2app64.dll"

from cmd you can try this:

regsvr32 "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\db2app64.dll"
regsvr32 "C:\Program Files\IBM\IBM DATA SERVER DRIVER\bin\db2app.dll"
  • Also, before doing these all, you need to make sure that connection between you and your vendor is open (what ever it's, VPN, Site to Site VPN and etc)
查看更多
别忘想泡老子
3楼-- · 2019-04-11 10:28

I'm going to jump in here and put my steps to fully automate the deploy of the .Net Data Providers from IBM since this is the first of MANY errors that I ran into attempting to get this to work. I hope someone avoids being put on the wheel of pain by reading these steps.

  1. Add the reference to the DB2 .Net Data Providers using nuget: https://www.nuget.org/packages/IBM.Data.DB2/ You will notice that it give you a hint of the pain you are about to endure: NOTICE - This packakge requires the IBM DB2 client software to be installed locally and available on your %PATH% environment variable.
  2. Next, you will need to set the DLLs that you just referenced not be included in the BIN folder by setting the DLLs to Copy Local FALSE by Right Clicking the references and going to properties.

    If these are put in the BIN you will get the error message:

    [BadImageFormatException: Could not load file or assembly 'IBM.Data.DB2' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

    IBM's reference to the issue: http://www-01.ibm.com/support/docview.wss?uid=swg21902663

  3. Now in order to begin developing you will need to install the driver packages or the run time client from here https://www-945.ibm.com/support/fixcentral/ (good luck) or you will be receiving the following errors:

    Exception Message: Could not load file or assembly 'IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208' or one of its dependencies. The system cannot find the file specified.

  4. Developing time! Have fun!
  5. Now you need to deploy and you realize you have a problem with automating installing a DB2 driver and start looking for solutions:

    How to properly deploy IBM DB2 data provider

    ASP.NET -- IIS7 -- IBM DB2 Issue

    Not wanting to manually install on DEV/QA/STG/PROD, I found the that you can install IBM Data Server Client Package silently via command line using a response file. About halfway down this page: https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.swg.im.dbclient.install.doc/doc/t0007315.html you will find the relevant information after this line:

    To install the IBM Data Server Driver Package software, run the v10.5fpX_ntxYY_dsdriver_EN command. The X represents the fix pack number value and the YY represents either 32-bit or 64-bit release.

    A link to a sample response file to copy and change with your information is here: https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.apdv.sample.doc/doc/dsdriver/s-dsdriver-rsp.html

  6. Now you are all set right? WRONG. Apparently a "successful install" doesn't include moving the appropriate DLLs to the GAC and registering them in the Registry. Thanks IBM: https://www-01.ibm.com/support/docview.wss?uid=swg21618434

    Not wanting to give up in my automated deploy dreams, I put all the above knowledge into a Powershell script:

    param([string]$driverPath,[string]$installPath)    
    
    [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
    $publish = New-Object System.EnterpriseServices.Internal.Publish    
    
    Write-output "Installing v11.1.2fp2a_ntx64_dsdriver_EN.exe"
    & "$driverPath\v11.1.2fp2a_ntx64_dsdriver_EN.exe" /u "$driverPath\DB2.cfg" /l "$driverPath\log.txt" | Out-null
    
    $publish.GacInstall("$installPath\bin\netf20\IBM.Data.DB2.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf20_32\IBM.Data.DB2.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf40\IBM.Data.DB2.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf40_32\IBM.Data.DB2.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf20\IBM.Data.DB2.Entity.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf20_32\IBM.Data.DB2.Entity.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf40\IBM.Data.DB2.Entity.dll") | Out-null
    $publish.GacInstall("$installPath\bin\netf40_32\IBM.Data.DB2.Entity.dll") | Out-null
    
  7. Now you will find that you are getting another error. Until now you were only fighting IBM.Data.DB2.*.dll issues. The following error is now present:

    Exception Message: Unable to load DLL 'db2app64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

    Come on IBM!! A quick Google search finds the same results we got with the #1. "Change your app to 32-bit" This is wrong, of course, we know better now!

    I'll help you out and let you know that this DLL was installed along with the OTHER DLLs, just in a different path!

    Just add them to the bottom of the Powershell script:

    $publish.GacInstall("$installPath\bin\db2app.dll") | Out-null
    $publish.GacInstall("$installPath\bin\db2app64.dll") | Out-null
    

Now you have a fully automated DB2 .Net Provider to connect to a DB2 Server.

Hope this helps.

EDIT: You can alternately isolate the required DLLs and only include those in a folder (like /lib/) and deploy those instead of installing with an exe. Reference the /lib/ location instead of the driver install location to move to the GAC.

查看更多
虎瘦雄心在
4楼-- · 2019-04-11 10:30

In my case, this was happening with Windows Server 2012 R2 Standard, This is an error caused by the package not installing in the Global Cache Assembly make sure you check C:\Windows\Assembly to ensure your program is installed properly under GAC_32 , GAC_64, you should see

enter image description here

If not this is what I had to do in order to solve it.

1)I had to check the version I had installed in order to ensure compatibility.

2)The "IBM DATA SERVER CLIENT" did not install properly, using the GUI due to a bug, so it had to be run silently using this as an administrator

3) Due to the server nature I had to install .Net 3.5 using IBM-Steps <-- Link That did not work for me so I had to use MS-Steps <-- Link with these lines

DISM /Image:C:\test\offline /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:D:\sources\sxs

4) Found out the one of the issues is that the "IBM DATA SERVER CLIENT" was still not install properly by checking the DB2LOG folder which is in your C:\User_Dir\Document\DB2LOG

5) Had to install the "IBM DATA RUN TIME CLIENT" latest fix in my case for 9.7 is fix 10, can be accessed free on IBM's download page you just have to register it takes 1 minute and you get a response asap, make sure you follow through else wait until your stupidity factor kicks in.... me...

6) Then I was able to install "IBM DATA SERVER CLIENT"

7) Double check to ensure you can see "DB2COPY1" or whatever you choose to call your installation copy.

enter image description here

I'm sure you don't need both, however I will no loginder mess with this thing.

查看更多
爷的心禁止访问
5楼-- · 2019-04-11 10:33

It sounds like you are trying to load a 32 bit compiled IBM DB2 driver from a .NET application running in 64 bit mode. Try changing configuration manager to run in x86 mode.

查看更多
Ridiculous、
6楼-- · 2019-04-11 10:44

I just recently had this problem. I found that the publishing process in Visual Studio was putting a copy of IBM.Data.DB2.dll in the BIN directory of the web application. When it is manually deleted, the web application starts operating correctly. I do not know why it's getting published, as it is not directly referenced.

The easiest way I've found to resolve this, is to use a Post-build event.

Project properties --> Build Events --> Post-build event command line

Here's the command line I'm using:

del $(TargetDir)\IBM.Data.DB2.dll
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-04-11 10:45

This problem happened me these days and it was solved following what this IBM document says, that is to go to the reference in the project and set "Copy local" to false for DB2 dlls, referencing diretly the ones from the DB2 client setup

查看更多
登录 后发表回答