Connect a .NET application running on Linux on Power to IBM i using ODBC

Connect a .NET application running on Linux on Power to IBM i using ODBC

Recently IBM has announced .NET 7 support on Linux on Power, now you can run .NET Applications on Power. This blog demonstrates how to connect a .NET 7 sample application running on Linux Power(ppc64le)  to a remote Db2 on IBM i server using ODBC connector.

Initial setup of the servers:

To complete the steps outline in this blog, you’ll need the following:

A partition running IBM i on an IBM Power server. It is assumed that you have an IBM i server already setup.
A partition running Linux (RHEL 8.7/9.1) on an IBM Power server. If you need access to Power hardware, this blog, Accelerate your open source development with access to IBM Power resources lists several free and low-cost options. Open Source developers might consider the OSL option.

NET 7 is available as the RPM, dotnet-sdk-7.0. in the AppStream repositories for RHEL 8.7 and RHEL 9.1 The AppStream repositories are enabled by default on RHEL 8/9 systems.


NOTE: The rest of the steps in this blog are run on the Linux on Power server.

Install ODBC drivers on the Linux on Power server

Install the IBM i Access ODBC Driver (see doc here). This installs ibm-iaccess and its dependencies, libtool-ltdl and unixODBC.

curl https://public.dhe.ibm.com/software/ibmi/products/odbc/rpms/ibmiacs.repo | sudo tee /etc/yum.repos.d/ibmiacs.repo

dnf install refresh ibmiaccess​

Verify your set up by running

odbcinst

commands as below:

odbcinst j

The output may look like:
unixODBC 2.3.7
DRIVERS…………: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size…….: 8
SQLLEN Size……..: 8
SQLSETPOSIROW Size.: 8
Next print the list of drivers using the command below

odbcinst q d
The output may look like:
[PostgreSQL]
[MySQL]
[FreeTDS]
[MariaDB]
[IBM i Access ODBC Driver]
[IBM i Access ODBC Driver 64-bit]
Look at the details of the IBM i Access ODBC Driver

odbcinst q d n “IBM i Access ODBC Driver 64-bit”
The output may look like: 
[IBM i Access ODBC Driver 64-bit]
Description=IBM i Access for Linux 64-bit ODBC Driver
Driver=/opt/ibm/iaccess/lib64/libcwbodbc.so
Setup=/opt/ibm/iaccess/lib64/libcwbodbcs.so
Threading=0
DontDLClose=1
UsageCount=1

Install .NET 7 on the Linux on Power server

Install dotnet7 using yum ( the server in the example is running RHEL 8.7 ):

yum install dotnet

Create .NET 7 console application on the Linux on Power server

Create the console application using the dotnet new command:
dotnet new console -o odbc_sample

Add the System.Data.Odbc package using the dotnet add command. This will add package reference in csproj file.
# Change directory to odbc_sample
cd odbc_sample

# Add the System.Data.Odbc Package to the project
dotnet add package System.Data.Odbc
 Note the included System.Data.Odbc PackageReference in the odbc_sample.csproj file. 
cat odbc_sample.csproj

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

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

<ItemGroup>
<PackageReference Include=”System.Data.Odbc” Version=”7.0.0″ />
</ItemGroup>

</Project>

Copy the code below into the Program.cs file. This sample connects to Db2 on IBM i and reads all rows and columns from qcustcdt table in the qiws schema and displays the same on console output.


Note:
See the format of
ConnectionString to connect to DB2 on IBM i. Be sure to modify the program to supply the connection parameters for your IBM i server. Also modify the program to retrieve data from tables on your IBM i server.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Replace connection string with your IBMi systems credentialss
string MyConString = “DRIVER={IBM i Access ODBC Driver 64-bit}; ” +
“SYSTEM=<<hostname of IBMi machine>>; ” +
“UID=<uid>; ” +
“PWD=<pwd>;”; // Open a Database Connection
OdbcConnection DbConnection = new OdbcConnection(MyConString);
DbConnection.Open();
OdbcCommand DbCommand = DbConnection.CreateCommand();
// Select rows from a table qiws.qcustcdt on IBMi
DbCommand.CommandText = “SELECT * FROM qiws.qcustcdt”;
OdbcDataReader DbReader = DbCommand.ExecuteReader();
// Display the retrieved data to the console
int fCount = DbReader.FieldCount;
Console.Write( “:” );
for ( int i = 0; i < fCount; i ++ )
{
String fName = DbReader.GetName(i);
Console.Write( fName + “:” );
}
Console.WriteLine();
while( DbReader.Read())
{
Console.Write( “:” );
for (int i = 0; i < fCount; i++)
{
String col = DbReader.GetString(i);
Console.Write(col + “:”);
}
Console.WriteLine();
}
// Close the Database Connection
DbReader.Close();
DbCommand.Dispose();
DbConnection.Close();
}
}
}

Rebuild the project:
dotnet build

Rerun the application:
dotnet run
The run may  fail with the error “System.Data.ODBC is not supported on this platform.” The output may look like:
Unhandled Exception:
System.PlatformNotSupportedException: System.Data.ODBC is not supported on this platform.
at System.Data.Odbc.OdbcConnection..ctor(String connectionString)
at ConsoleApplication1.Program.Main(String[] args) in /root/odbc_sample/Program.cs:line 18
[ERROR] FATAL UNHANDLED EXCEPTION: System.PlatformNotSupportedException: System.Data.ODBC is not supported on this platform.
at System.Data.Odbc.OdbcConnection..ctor(String connectionString)
at ConsoleApplication1.Program.Main(String[] args) in /root/odbc_sample/Program.cs:line 18

This is a known problem, see issue . The fix should be available in the next .NET7 service release. In the meantime, as a workaround, you have to add a runtimes section to the end of /usr/lib64/dotnet/shared/Microsoft.NETCore.App/7.0.0/Microsoft.NETCore.App.deps.json as shown below:

diff –git a/Microsoft.NETCore.App.deps.json b/Microsoft.NETCore.App.deps.json.orig
index 75a6e51..eba6595 100644
— a/Microsoft.NETCore.App.deps.json.orig
+++ b/Microsoft.NETCore.App.deps.json
@@ -733,18 +733,4 @@
“sha512”: “”,
“path”: “microsoft.netcore.app.runtime.rhel.8-ppc64le/7.0.0”
}
+ },
+ “runtimes”: {
+ “rhel.8-ppc64le”: [
+ “rhel.8”,
+ “rhel-ppc64le”,
+ “rhel”,
+ “linux-ppc64le”,
+ “linux”,
+ “unix-ppc64le”,
+ “unix”,
+ “any”,
+ “base”

+ ]
+ }
}
}

After this change, rebuild and rerun the application and this time it displays the content of qcustcdt table in the QIWS schema as shown below:
dotnet build
dotnet run
The program should display the content of the qcustcdt table in the QIWS schema as below
:CUSNUM:LSTNAM:INIT:STREET:CITY:STATE:ZIPCOD:CDTLMT:CHGCOD:BALDUE:CDTDUE:
:938472:Henning :G K:4859 Elm Ave :Dallas:TX:75217:5000:3:37.00:0:
:839283:Jones :B D:21B NW 135 St:Clay :NY:13041:400:1:100.00:0:
:392859:Vine :S S:PO Box 79 :Broton:VT:5046:700:1:439.00:0:
:938485:Johnson :J A:3 Alpine Way :Helen :GA:30545:9999:2:3987.50:33.50:
:397267:Tyron :W E:13 Myrtle Dr :Hector:NY:14841:1000:1:0:0:
:846283:Alison :J S:787 Lake Dr :Isle :MN:56342:5000:3:10.00:0:
:693829:Thomas :A N:3 Dove Circle:Casper:WY:82609:9999:2:0:0:
:593029:Williams:E D:485 SE 2 Ave :Dallas:TX:75218:200:1:25.00:0:
:583990:Abraham :M T:392 Mill St :Isle :MN:56342:9999:3:500.00:0:
:123456:Wold :J K:Makle St :Rock :MN:55904:788:2:0:0:


Conclusion

With the ability to connect from .NET applications on Linux on Power to data residing on an IBM i server, IBM Power customer will have the ability to modernize their apps while running on the same Power platform as their IBM i business databases. Applications can now be moved closer to the data on their Power servers.

Expand on the program in this demo to experiment with connecting to your business databases.

References

IBM-provided Repositories ODBC Linux Driver Repositories

Using ODBC to connect to IBM i

Known Issue: Unhandled Exception: System.PlatformNotSupportedException: System.Data.ODBC is not supported on this platform

New research aims to analyze how widespread COBOL is • The Register

The Open Mainframe Project’s COBOL Working Group is seeking to discover just how much the venerable programming language is still being used in the industry, and it would like your help.

Yes, the Common Business-Oriented Language may be over 60 years old, but it is still widely employed and continues to play an active role in running the global economy, according to the COBOL Working Group, which was set up in 2020 by the Open Mainframe Project to promote and support its continued use.

This was shown in a report earlier this year from Micro Focus, the UK software outfit that still does much business around support for COBOL, which found that the amount of code in daily use worldwide is somewhere in the region of 775-850 billion lines, and that 92 percent of organizations it surveyed regarded COBOL as a strategic technology.

That could be a problem as the long history of the language means that many practitioners are beyond retirement age, as The Register noted earlier this year when reporting on the launch of a new open source compiler for it.

In fact, at the start of the pandemic, the governor of New Jersey had to appeal for those with COBOL skills to come forward and help as the state’s mainframe computers struggled under the weight of requests for help from citizens.

Now it seems that the COBOL Working Group wants to find out for itself how widespread the use of the language is, and is looking to conduct its own survey in partnership with The Linux Foundation Research and the Linux Foundation Training and Certification.

Micro Focus prepares for private ownership, turnaround still WIP
TypeScript joins 5 most used languages in 2022 lineup
Blockchain powered stock market rebuild started in 2017 delayed again
An open-source COBOL contender emerges

The project aims to collect continued usage statistics of the COBOL language in the IT industry, as well as assess the value of continued vendor investments in the language in order to support modern day use cases in the digital era. Another key area it aims to collect information on is current attitudes to future use-cases for COBOL applications.

According to the COBOL Working Group, it hopes to gain insights from the survey across all relevant industries and geographic regions to provide a fully representative overview of the current state of play.

Naturally, the COBOL Working Group would love it if some kind benefactors would be willing to help contribute to the funding for this project.

“We seek your support and investment to bring this exciting project – and significant brand opportunity – to fruition,” wrote the Open Mainframe Project’s Director of Program Management, John Mertic, announcing the research on the project’s blog.

The Open Mainframe Project itself was founded in 2015, with the help of IBM, to promote the deployment and use of Linux and open source in mainframe computing environments. It is managed by The Linux Foundation. ®

As a member of #COMMON – your voice matters! During #MoM, you can voice your concerns, ask your questions, and vote for board members. MoM is happening on April 27th @ #POWERUp2023. REGISTER TODAY, and we will see you there! (Psst! It’s still $400 off!)

As a member of #COMMON – your voice matters! During #MoM, you can voice your concerns, ask your questions, and vote for board members. MoM is happening on April 27th @ #POWERUp2023. REGISTER TODAY, and we will see you there! (Psst! It’s still $400 off!) bit.ly/3i5q8Lr pic.twitter.com/tt5rXuO7GG

– COMMON (@COMMONug)08:01 – Dec 14, 2022

Did you miss last month’s in-person Toronto User Group (TUG) group? Amazing presentation – BOB (Better Object Building), from IBM’s @EdmundReinhardt Edmund also introduced Rising Star Charlie Hird, developer AAG – #Nagios #Monitoring for #IBMi

Did you miss last month’s in-person Toronto User Group (TUG) group? Amazing presentation – BOB (Better Object Building), from IBM’s @EdmundReinhardt Edmund also introduced Rising Star Charlie Hird, developer AAG – #Nagios #Monitoring for #IBMi shieldadvanced.com/Products.php pic.twitter.com/c1BHGkduvw

– Alison Hird (@AHird)06:10 – Dec 14, 2022

Make 2023 the year to modernise, upgrade, develop, support, test, integrate and enhance your @IBM i (AS400) systems – with KFA’s help, take a look how: #IBM #IBMiDevelopment #IBMiIntegration #IBMiSupport #IBMiTesting #IBMiecommerceIntegration

Make 2023 the year to modernise, upgrade, develop, support, test, integrate and enhance your @IBM i (AS400) systems – with KFA’s help, take a look how: tinyurl.com/jrzk47nh
#IBMi #IBM #IBMiDevelopment #IBMiIntegration #IBMiSupport #IBMiTesting #IBMiecommerceIntegration pic.twitter.com/C0wBXJLDJV

– KFA Connect (@KFAConnect)04:41 – Dec 14, 2022

Verified by MonsterInsights