Skip to content

thesharpninjas/Ninja.Sharp.OpenDb2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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