Search code examples
c#.netmacosdb2

How to use Net.IBM.Data.Db2 on Mac OSX?


I have a dotnet core project that uses Net.IBM.Data.Db2 and it runs fine on both Windows and Linux. I am now trying to get it to run on an Apple Macbook M3 Pro using Net.IBM.Data.Db2-osx, but I'm getting the following run time error:

System.IO.FileNotFoundException: Could not load file or assembly 'IBM.Data.Db2, Version=8.0.0.200, Culture=neutral, PublicKeyToken=7c307b91aa13d208'. The system cannot find the file specified.

My console app contains the following code.

ConsoleApp1.csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Dapper" Version="2.1.37" />
        <!-- <PackageReference Include="Net.IBM.Data.Db2" Version="8.0.0.200" Condition="$([MSBuild]::IsOsPlatform('Windows'))" /> -->
        <!-- <PackageReference Include="Net.IBM.Data.Db2-lnx" Version="8.0.0.200" Condition="$([MSBuild]::IsOsPlatform('Linux'))" /> -->
        <!-- <PackageReference Include="Net.IBM.Data.Db2-osx" Version="8.0.0.200" Condition="$([MSBuild]::IsOsPlatform('OSX'))" /> -->
        <PackageReference Include="Net.IBM.Data.Db2-osx" Version="8.0.0.200" />
</ItemGroup>

</Project>

Program.cs:

// See https://aka.ms/new-console-template for more information
using IBM.Data.Db2; // note: this is correct, Net.IBM.Data.Db2 does not work
using Dapper;
using System.Data;

Console.WriteLine("Start");

string CONNECTION_STRING = "Server=db.foo.com:50000;Database=foo;UID=foo;PWD=bar;";

var sql = "select * from ST004.students";
using (var connection = new DB2Connection(CONNECTION_STRING))
{
    var rows = await connection.QueryAsync(sql);
    Console.WriteLine(rows);
}

Console.WriteLine("End");

The above throws the following run time error:

Exception has occurred: CLR/System.IO.FileNotFoundException
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll: 'Could not load file or assembly 'IBM.Data.Db2, Version=8.0.0.200, Culture=neutral, PublicKeyToken=7c307b91aa13d208'. The system cannot find the file specified.'
   at Program.<<Main>$>d__0.MoveNext() in /Users/me/dev/ConsoleApp1/ConsoleApp1/Program.cs:line 17
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at Program.<Main>$(String[] args) in /Users/me/dev/ConsoleApp1/ConsoleApp1/Program.cs:line 6
   at Program.<Main>(String[] args)

Solution

  • Reason for the error is version 8.0.0.200 of Net.IBM.Data.Db2-osx does not support ARM architecture.

    Version 9.0.0.100 of Net.IBM.Data.Db2-osx has been recently released and now has ARM support.