System.BadImageFormatException while connecting to DB2 from .Net - c#

I have DB2 server that I'm trying to connect from my machine, but I keep getting the below error when I start the application, before even start the connection
System.BadImageFormatException: 'Could not load file or assembly 'IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208' or one of its dependencies. An attempt was made to load a program with an incorrect format.'
please not that I don't have DB2 driver installed on my machine. but I refer to the below libraries in my application, which I got them from DB2 database server.
IBM.Data.DB2;
IBM.Data.DB2.Entity;
IBM.Data.Informix;

Normally the error message is caused by a 32/64 bit conflict.
Your program could be compiled in 64 bit (or any) and the drivers in 32 bit or the other way round.
Check the Configuration Manager and set the Platform to x86 or x64.

Related

Common Oracle Client Connection Issue with a Console .NET 4.7 program: Could not load file or assembly 'Oracle.DataAccess, Version=2.121.2.0

I download the Oracle client was downloaded from https://www.oracle.com/database/technologies/oracle19c-windows-downloads.html and select the file "NT_193000_client.zip" under the heading "Oracle Database 19c Client (19.3) for Microsoft Windows (32-bit)".
I then installed it on my server. When I run a TNSPing command to check the version number and bit-ness of the Oracle Client installed, I get:
TNS Ping Utility for 32-bit Windows: Version 19.0.0.0.0 - Production on 20-OCT-2022 22:50:49
I then used SQLPlus to verify that I could connect to the database.
Next, I compiled the following simple C# program that tests Oracle connectivity. The program was compiled as a 32-bit .NET 6 program.
using System;
namespace TestOracle
{
internal class Program
{
static void Main(string[] args)
{
string cs = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=servernm.sys.myco.com)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MySERVICE.MYCO.com)));User Id=MYID;Password=MYPWD;Validate Connection=True;";
try
{
int count = Db.TestConnectivity(cs);
Console.WriteLine($"Count={count}");
}
catch (Exception e)
{
Console.WriteLine(e);
}
var key = Console.ReadKey();
}
}
}
using Oracle.DataAccess.Client;
using System;
using System.Data;
using System.Text;
namespace TestOracle
{
public static class Db
{
public static int TestConnectivity(string connectionString)
{
var sqlStringBuilder = new StringBuilder();
sqlStringBuilder.AppendLine($"SELECT 1 AS RecordCount FROM DUAL");
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
OracleCommand command = connection.CreateCommand();
command.Connection = connection;
var oleDbDataAdapter = new OracleDataAdapter(command);
command.CommandText = sqlStringBuilder.ToString();
DataSet dataset = new DataSet();
oleDbDataAdapter.Fill(dataset);
return Convert.ToInt32(dataset.Tables[0].Rows[0]["RecordCount"]);
}
}
}
}
When I ran the program, I get this error:
System.IO.FileNotFoundException: Could not load file or assembly 'Oracle.DataAccess, Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. The system cannot find the file specified.
File name: 'Oracle.DataAccess, Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342'
at TestOracle.Db.TestConnectivity(String connectionString)
at TestOracle.Program.Main(String[] args) in C:\Users\C39293\OneDrive - CIGNA\Documents\Visual Studio 2015\Projects\Dokmanovich.File2Table\TestOracle\Program.cs:line 13
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
I know that this is a common error and that the common issue is a mismatch of bitness or a version problem. In my case, the bitness of my program, 32, matches the bitness of the client installed. Furthermore, I expect that if the program runs on my machine with an Oracle client of 12.1, that a higher level client, 19, should also work. Is this assumption true? I was unable to find the exact same oracle Client.
What am I missing?
Update
When I looked in my C# project References, I saw that the Oracle.DataAccess.dll pointed to my 64 Oracle client folder on my machine, not my 32 bit one ( I have both clients). Since I want to run on a server that only has the 32 bit Oracle client, I changed the reference to point to the 32 bit dll and re-compiled it as before, as a 32 bit program.
When I reran the program on the server, I got a different error:
System.TypeInitializationException: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception. ---> Oracle.DataAccess.Client.OracleException: The provider is not compatible with the version of Oracle client
at Oracle.DataAccess.Client.OracleInit.Initialize()
at Oracle.DataAccess.Client.OracleConnection..cctor()
--- End of inner exception stack trace ---
at Oracle.DataAccess.Client.OracleConnection..ctor(String connectionString)
at TestOracle.Db.TestConnectivity(String connectionString) in C:\Users\Myid\OneDrive - CIGNA\Documents\Visual Studio 2015\Projects\MyName.File2Table\TestOracle\Db.cs:line 15
at TestOracle.Program.Main(String[] args) in C:\Users\MyId\OneDrive - CIGNA\Documents\Visual Studio 2015\Projects\MyName.File2Table\TestOracle\Program.cs:line 13
I guess this is suggesting that I have the wrong Client. What should I have installed so this code runs on the server?
Update 2
I had two version of the 32 Oracle.DataAccess.dll, version 2.121.20 and 4.121.2.0. The error was the same for both (the error message immediately above).
You may consider to use the Oracle Data Provider for .NET, Managed Driver (ODP.NET Managed Driver), then you have to copy only the Oracle.ManagedDataAccess.dll file without any further dependencies. It works for both, 32bit and 64bit.
Apart from 32/64 bit you need to consider a few more things.
If you like to use Oracle.DataAccess Version=2.xxx, then you must compile to .NET Framework 2.0. If you compile to .NET Framework 4.0 or higher, then the application will try to load Oracle.DataAccess Version=4.xxx (unless you load assemblies manually with Assembly.Load())
The version of Oracle.DataAccess has to match exactly the version of the Oracle client. Using version 2.121.x or 4.121.x with a Oracle 19 client does not work.
Assemblies (i.e. *.dll files) are loaded either from GAC or from folder of the application .exe. Well, in reality it is more complex (see How the Runtime Locates Assemblies), but let's keep it simple.
The DLL files from GAC take always precedence over the local files. However, in 12.1 and later ODP.NET is not added to GAC anymore by the installer. You need to add them manually, for example with the gacutil tool.
Looks like on your machine you had a 64-bit Oracle client installed. If you like to use 32-bit and 64-bit client on the same machine, follow this instruction: BadImageFormatException. This will occur when running in 64 bit mode with the 32 bit Oracle client components installed
You may also have a look at my Oracle Connection Tester, it should give you a hint where the problem is.

The type initializer for 'Sap.Data.Hana.HanaConnection' threw an exception. ---> System.IO.FileNotFoundException: Cannot find libADONETHDB.dll

I have a problem only on a specific machine.
I have two programs with a reference to Sap.Data.Hana.v4.5.dll, the ADO.NET Provider for .NET 4.5 for HANA database.
When my programs instantiates a connection object with
dbConnection = new HanaConnection(...);
I get this error:
System.TypeInitializationException: The type initializer for 'Sap.Data.Hana.HanaConnection' threw an exception. ---> System.IO.FileNotFoundException: Cannot find libADONETHDB.dll.
at Sap.Data.Hana.HanaUnmanagedDll.SearchNativeDlls(String regKeyName)
at Sap.Data.Hana.HanaUnmanagedDll..ctor()
at Sap.Data.Hana.HanaUnmanagedDll.get_Instance()
at Sap.Data.Hana.HanaConnection..cctor()
--- End of inner exception stack trace ---
at [...my program calls...]
Why can't it find libADONETHDB.dll?
My applications are build one in 32-bit, the other in 64-bit.
On this machine I installed SAP HANA client 2.8.20.23662 (latest version) both 32-bit and 64-bit.
The file that cannot be found seems to exist:
You could use Process Monitor while running the relevant part of the program to see, which file is missing and the path it should be in. Exclude all other events than file events and filter out all SUCCESS messages.
Perhaps it is referenced relative to the calling DLL (where Sap.Data.Hana.HanaUnmanagedDll.SearchNativeDlls is defined).
My hipotesis is that GAC had been corrupted with version 1 of Sap.Data.Hana.v4.5.dll:
Probably in the past HANA Client version 1 had been installed and removed and uninstallation hadn't cleaned the GAC.
My program uses HANA Client of any version, works both with version 1 and 2. For some reason when they start they seem to take version 1 of Sap.Data.Hana.v4.5.dll in the GAC. Process Monitor (thanks #sc911) confirms it:
This .NET DLL then looks for some unmanaged DLLs in C:\Program Files (x86)\hdbclient. Version 1 seems to look for libADONETHDB.dll in root folder.
But currently in this machine in that folder we have HANA Client 2.x and its folder structure is quite different from that of version 1 (there is no libADONETHDB.dll in root folder).
Hence the error in this question.
All this is valid both for 32-bit and 64-bit HANA Client.
I solved renaming or deleting version 1 folder in C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Sap.Data.Hana.v4.5 (probably there are better ways for performing this cleanup).

EmguCV: Unable to load DLL 'cvextern': The specified procedure could not be found

When publishing project to windows server 2003 (old test machine) I get following error when trying to call open cv functions.
Additional information: The type initializer for 'Emgu.CV.CvInvoke' threw an exception.Unable to load DLL 'cvextern': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)
I am using EmguCV 3.4.1 version with uploaded libraries:
vcruntime140.dll
opencv_ffmpeg341.dll
msvcp140.dll
cvextern.dll
concrt140.dll
To the Emgu package nothing extra (dll's) were added and on developer machine I could develop the application.
I keep wondering whether additional native dll's should be added. I downloaded binaries for 3.4.1 version. The files go like that:
opencv_core249.dll
opencv_highgui249.dll
Those two above where in 3.4.1 package. When i copied them to program directory it didn't help either.
What's more I have no idea how to track down the problem.
Process monitor doesn't show that the process is looking for something that cannot be loaded.
In the inner exception I am given only this exception: Unable to load DLL 'cvextern': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)
I had to install "Desktop Experience" feature on my server. Because server 2003 doesn't seem to have it, I had to move it to another server 2008 host.

Could not load file or assembly 'LinqToExcel, Version

I am trying to read an xlsx file.
I got exception that
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. c#
then I installed it from here
http://www.microsoft.com/en-us/download/details.aspx?id=13255
then I changed the platform target to x64
now i got this exception:
BadImageFormatException was unhandeled
{"Could not load file or assembly 'LinqToExcel, Version=1.9.0.0, Culture=neutral, PublicKeyToken=9c8ea65a58f03f1f' or one of its dependencies. An attempt was made to load a program with an incorrect format."}
Update 1
I already installed the linq_to_excel
using
Install-Package LinqToExcel
and then add the lib files to references
You have two options to solve
Enable IIS to run 32 bits app
Install LinqToExcel_x64
if change 32 bit app in iis From false to true,all conflicts about exteltolinq version solve
Please install Microsoft office in your machine it will automatically solve your problem. It worked for me as well.

Could not load file or assembly 'System.Data.DataSetExtensions, Version=3.5.0.0

I have a C# Console Application that uses System.Data.DataSetExtensions that I can run smoothly on the client's server through the command line. The only thing I did was to copy the bin/Release directory contents to a specific directory on the server and run the app through the cmd.
Now, I want to run the same app through an SQL SERVER Job. I've tried both the CmdExec and T-SQL options, the latest using xp_cmdshell. I've also tried running the program without the job, just with the xp_cmdshell on a new query tab.
I always get this error:
Unhandled Exception: System.IO.FileNotFoundException: Could not load
file or assembly 'System.Data.DataSetExtensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its
dependencies. The system cannot find the file specified.
Can someone help me to fix this? Why can I run the program through the cmd line and not through Sql Server?
Thx a lot!
Make sure that the database server (where SQL Server was installed) has .NET Framework 3.5 installed.

Categories

Resources