Ninja.Sharp.OpenDb2 1.2.0

dotnet add package Ninja.Sharp.OpenDb2 --version 1.2.0
                    
NuGet\Install-Package Ninja.Sharp.OpenDb2 -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ninja.Sharp.OpenDb2" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ninja.Sharp.OpenDb2" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Ninja.Sharp.OpenDb2" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ninja.Sharp.OpenDb2 --version 1.2.0
                    
#r "nuget: Ninja.Sharp.OpenDb2, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Ninja.Sharp.OpenDb2&version=1.2.0
                    
Install Ninja.Sharp.OpenDb2 as a Cake Addin
#tool nuget:?package=Ninja.Sharp.OpenDb2&version=1.2.0
                    
Install Ninja.Sharp.OpenDb2 as a Cake Tool

License: MIT Nuget issues - Ninja.Sharp.OpenDb2 stars - Ninja.Sharp.OpenDb2

Ninja.Sharp.OpenDb2

This .NET library provides an abstraction layer over DB2 database access methods, facilitating dependency injection and enabling easier mocking and testing of database interactions.

Packages


Package NuGet Stable
OpenDb2 OpenDb2

Generic Service registration

You can register the OpenDB2 service simply with dependency injection:

builder.services
   .AddDb2Services(connectionString, configuration);

Usage - On Windows

Windows Service registration:

builder.services
   .AddWinDb2Services(connectionString, configuration);

Calling stored procedure:

public class Db2Repository(IWinDb2Connection connection)
{
    public async Task DoSomething(string param1, int param2)
    {
        using (connection)
        {
            await connection.Open();

            // Creating command

            using var cmd = connection.CreateCommand("STORED_PROCEDURE_NAME", CommandType.StoredProcedure);

            // Creating parameters

            cmd.AddParam("@PARAM1", WinDb2Type.VarChar, 10, param1);
            cmd.AddParam("@PARAM2", WinDb2Type.Decimal, 6, param2);
            cmd.AddParam("@OUTPARAM", WinDb2Type.VarChar, 250, ParameterDirection.Output);

            // Reading output parameter

            await cmd.ExecuteNonQuery();

            string? outputResult = cmd.ReadParam("@OUTPARAM") as string;

            // Data retrieval from Reader

            using var reader = await cmd.ExecuteReader();

            while (await reader.ReadAsync())
            {
                var field1 = reader.GetValue("FIELD1").ToString();
                var field2 = reader.GetValue("FIELD2").ToString();
            }
        }
    }
}

Executing query with transaction and DataAdapter

public class Db2Repository(IWinDb2Connection connection)
{
    public async Task DoSomething()
    {
        using (connection)
        {
            await connection.Open();

            using var transaction = connection.BeginTransaction();

            try
            {
                // Creating command

                using var cmd = connection.CreateCommand("QUERY", CommandType.Text, transaction);

                // Creating data adapter

                using var adapter = cmd.CreateDataAdapter();

                // Filling and reading DataTable

                DataTable dt = new();

                adapter.Fill(dt);

                var output = dt
                    .AsEnumerable()
                    .FirstOrDefault(row => row.Field<string>("FIELD1") == "VALUE");

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
            }
        }
    }
}

Usage - On Linux

Linux Service registration:

builder.services
   .AddLnxDb2Services(connectionString, configuration);

Calling stored procedure:

public class Db2Repository(ILnxDb2Connection connection)
{
    public async Task DoSomething(string param1, int param2)
    {
        using (connection)
        {
            await connection.Open();

            // Creating command

            using var cmd = connection.CreateCommand("STORED_PROCEDURE_NAME", CommandType.StoredProcedure);

            // Creating parameters

            cmd.AddParam("@PARAM1", LnxDb2Type.NVarChar, 10, param1);
            cmd.AddParam("@PARAM2", LnxDb2Type.Decimal, 6, param2);
            cmd.AddParam("@OUTPARAM", LnxDb2Type.VarChar, 250, ParameterDirection.Output);

            // Reading output parameter

            await cmd.ExecuteNonQuery();

            string? outputResult = cmd.ReadParam("@OUTPARAM") as string;

            // Data retrieval from Reader

            using var reader = await cmd.ExecuteReader();

            while (await reader.ReadAsync())
            {
                var field1 = reader.GetValue("FIELD1").ToString();
                var field2 = reader.GetValue("FIELD2").ToString();
            }
        }
    }
}

Executing query with transaction and DataAdapter

public class Db2Repository(ILnxDb2Connection connection)
{
    public async Task DoSomething()
    {
        using (connection)
        {
            await connection.Open();

            using var transaction = connection.BeginTransaction();

            try
            {
                // Creating command

                using var cmd = connection.CreateCommand("QUERY", CommandType.Text, transaction);

                // Creating data adapter

                using var adapter = cmd.CreateDataAdapter();

                // Filling and reading DataTable

                DataTable dt = new();

                adapter.Fill(dt);

                var output = dt
                    .AsEnumerable()
                    .FirstOrDefault(row => row.Field<string>("FIELD1") == "VALUE");

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
            }
        }
    }
}

Licensee

Repository source code is available under MIT License, see license in the source.

Contributing

Thank you for considering to help out with the source code! If you'd like to contribute, please fork, fix, commit and send a pull request for the maintainers to review and merge into the main code base.

Getting started with Git and GitHub

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 152 3/20/2025
1.1.0 217 7/3/2024
1.0.0 107 6/28/2024