Ninja.Sharp.OpenDb2 1.1.0

dotnet add package Ninja.Sharp.OpenDb2 --version 1.1.0                
NuGet\Install-Package Ninja.Sharp.OpenDb2 -Version 1.1.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.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ninja.Sharp.OpenDb2 --version 1.1.0                
#r "nuget: Ninja.Sharp.OpenDb2, 1.1.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.
// Install Ninja.Sharp.OpenDb2 as a Cake Addin
#addin nuget:?package=Ninja.Sharp.OpenDb2&version=1.1.0

// Install Ninja.Sharp.OpenDb2 as a Cake Tool
#tool nuget:?package=Ninja.Sharp.OpenDb2&version=1.1.0                

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

Usage - On Windows

Services 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

Services 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();
            }
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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.1.0 115 7/3/2024
1.0.0 90 6/28/2024