VTNET.Vitado 7.1.0

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

// Install VTNET.Vitado as a Cake Tool
#tool nuget:?package=VTNET.Vitado&version=7.1.0

ADO.NET Wrapper Library

Introduction

This library is designed to simplify ADO.NET interactions by wrapping around ADO.NET and using the SqlDataReader class to read data. It provides a set of methods for executing queries and stored procedures and mapping the results to objects, making database interactions in your .NET applications more straightforward and efficient.

Commands

The library offers a variety of commands to execute and interact with your database:

New Version

I no longer differentiate between 'Query' and 'Stored', now there’s only one keyword: 'Execute.'

Execute

  • Execute: Execute and return the number of affected rows.
  • Execute<T>: Execute and return a list of mapped data.
  • ExecuteAsync: Asynchronously execute and return the number of affected rows.
  • ExecuteAsync<T>: Asynchronously execute and return a list of mapped data.
  • ExecuteTable: Execute and return the result as a DataTable.
  • ExecuteTableAsync: Asynchronously execute and return the result as a DataTable.
  • ExecuteFirst<T>: Execute and return the first row as a mapped object.
  • ExecuteFirstAsync<T>: Asynchronously execute and return the first row as a mapped object.
  • ExecuteOne<T>: Execute and return the first cell of the first row as a mapped object.
  • ExecuteOneAsync<T>: Asynchronously execute and return the first cell of the first row as a mapped object.

New Class Structure

  • VitProviderBase: Defining the common architecture for 'Execution'
    • ExecuteCore: this is the core of all methods, you must inherit this method and handle data execute
      //example for inherit ExecuteCore
      protected override T ExecuteCore<T>(SqlCommand command, Func<SqlCommand, T> func)
      {
          //VitProviderBase does not contain a Connection. Therefore, you must specify a connection for the command.
          using SqlConnection connection = new(_connectionString);
          command.Connection = connection;
          //VitProviderBase does not handle life circle connection. Therefore, you must open connection.
          connection.Open(); 
          try
          {
              command.Connection = connection;
              return func(command);
          }
          catch (SqlException)
          {
              connection.Close();
              throw;
          }
          //VitProviderBase does not handle life circle connection. Therefore, you must Close connection.
          finally { connection.Close(); }
      }
      
  • VitProvider inherit VitProviderBase: handle life circle connection and add define all Execute methods.
  • VitProviderScope inherit VitProvider: handle life circle scope. Connection of VitProviderScope don't close when end method, you have to close the connection yourself or use 'using'
    db.Dispose(); //Closed by yourself.
    
    using db = new VitProviderScope(connectString); //The connection will close when the scope ends.
    
  • VitProviderBeta inherit VitProvider: this class will contain experimental code snippets.
  • VitProviderScope inherit VitProviderBeta: like VitProviderScope. But, this is beta.
  • DBService:

All the classes above allow you to inherit and modify them as you wish.

Example for custom VitProvider

public class VitProviderScope : VitProvider
{
    public VitProvider(string connectionString) : base(connectionString){}
    protected override T ExecuteCore<T>(SqlCommand command, Func<SqlCommand, T> func)
    {
        //Your code...
        return base.ExecuteCore(command, func);
    }
}

You can now seamlessly work with your database using this ADO.NET wrapper library, saving time and effort in your .NET applications.

Old Version

Query

  • Query: Execute a query and return the number of affected rows.
  • Query<T>: Execute a query and return a list of mapped data.
  • QueryAsync: Asynchronously execute a query and return the number of affected rows.
  • QueryAsync<T>: Asynchronously execute a query and return a list of mapped data.
  • QueryTable: Execute a query and return the result as a DataTable.
  • QueryTableAsync: Asynchronously execute a query and return the result as a DataTable.
  • QueryFirst<T>: Execute a query and return the first row as a mapped object.
  • QueryFirstAsync<T>: Asynchronously execute a query and return the first row as a mapped object.
  • QueryOne<T>: Execute a query and return the first cell of the first row as a mapped object.
  • QueryOneAsync<T>: Asynchronously execute a query and return the first cell of the first row as a mapped object.

Stored Procedure

  • Stored: Execute a stored procedure and return the number of affected rows.
  • Stored<T>: Execute a stored procedure and return a list of mapped data.
  • StoredAsync: Asynchronously execute a stored procedure and return the number of affected rows.
  • StoredAsync<T>: Asynchronously execute a stored procedure and return a list of mapped data.
  • StoredTable: Execute a stored procedure and return the result as a DataTable.
  • StoredTableAsync: Asynchronously execute a stored procedure and return the result as a DataTable.
  • StoredFirst<T>: Execute a stored procedure and return the first row as a mapped object.
  • StoredFirstAsync<T>: Asynchronously execute a stored procedure and return the first row as a mapped object.
  • StoredOne<T>: Execute a stored procedure and return the first cell of the first row as a mapped object.
  • StoredOneAsync<T>: Asynchronously execute a stored procedure and return the first cell of the first row as a mapped object.

The Motivation

The genesis of this library stems from the challenges encountered during a project that required database-first development. The project demanded dealing with complex database requirements, such as seamlessly adapting to changes like renaming parameters in stored procedures without necessitating corresponding modifications in C# code, handling table alterations like adding or removing columns without affecting the C# Insert and Update logic, and more. In essence, the project's data structure was highly dynamic and prone to alterations.

While searching for existing libraries to address these unique demands, it became evident that finding a suitable solution was a daunting task (or my search skills were not up to the challenge! 🤣) Consequently. I embarked on the journey to develop this library to cater to the project's specific needs and ensure a smoother development process. 👌

Integration with ASP/Blazor

You can easily integrate this library into your ASP.NET or Blazor applications. To get started, add the following code to your Startup.cs or Program.cs:

builder.Services.AddVitado("Data Source=.;Initial Catalog=...;MultipleActiveResultSets=True;User Id=...;Password=...");

Make sure to replace the connection string with your specific database details.

How to Use

Query

Here are examples of how to use the library for querying:

// Query and map data
List<DBOrderTable> fbBanLeTam = await db.QueryAsync<DBOrderTable>("SELECT * FROM [dbo].[FB_BANLE_TAM]");
DBOrderTable fbBanLeTam = await db.QueryFirstAsync<DBOrderTable>("SELECT TOP 1 * FROM [dbo].[FB_BANLE_TAM]");
string name = await db.QueryOneAsync<string>("SELECT TOP 1 * FROM [dbo].[FB_BANLE_TAM]");

// Parameters
var inforUser = await db.QueryFirstAsync<DB_SYS_USER>(
    query: "SELECT [UserName], [FullName], [Email], [PassWD], [UID], [Is_Employee], [Manv], [Must_Chg_Pwd] FROM [ERP_SOFTZ].[dbo].[SYS_USER] WHERE UID = @UID",
    paramsIn: new(("@UID", 241))
);

Stored Procedure

For using stored procedures, follow these examples:

// Query and map data
List<DBOrderTable> fbBanLeTam = await db.StoredAsync<DBOrderTable>("[dbo].[SYS_ReportParams]");
DBOrderTable fbBanLeTam = await db.StoredFirstAsync<DBOrderTable>("[dbo].[SYS_ReportParams]");

// Parameters
var inforUser = await db.QueryFirstAsync<DB_SYS_USER>(
    stored: "[dbo].[SYS_ReportParams]",
    paramsIn: new(("@UID", 241))
);

// Parameters with Output
var dataStored1 = db.StoredTable("[dbo].[SYS_ReportParams]", new(("@UID", 241), ("CODE", "NV1001")), new(("@Name", ""), ("@TestOutParams", 0)));

Get Parameters Of Stored Procedure And Mapping Type

var vitParams = db.StoredParams("[dbo].[SYS_ReportParams]");
foreach (var item in vitParams)
{
    var type = item.Type.GetTypeMap(); // mapping SqlDbType to Type
    var defaultValue = type.GetDefaultValue(); // get default value of type
    Console.WriteLine($"Type:{type}, ValueDefault:{defaultValue}");
}

Solutions For DynamicData

// Execute stored procedures without regard to parameters
var dataStored = db.StoredTable("[dbo].[Login]", new("Username","Password", true));

// Execute stored procedures without regard to parameters but with paramsout
var vitParamsAuto = db.StoredParams("[dbo].[SYS_ReportParams]").ToVitParamsAuto();
var dataStored1 = db.StoredTable("[dbo].[SYS_ReportParams]", vitParamsAuto.Item1, vitParamsAuto.Item2);

// Insert and update
var dataTable = await db.QueryTableAsync("select * from [dbo].[Customers]");
var vitG = new VitTrack("Customers", dataTable.Rows[0]);
vitG.Row["Name"] = "insert name";
var insert = vitG.Insert();
db.Execute(insert);
vitG.Row["Email"] = "update@email.com";
var update = vitG.Update(conditionColumn:"Name");
db.Execute(update);

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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
7.1.0 79 5/7/2024
7.0.2 103 2/28/2024
7.0.1 110 2/21/2024
7.0.0 88 2/21/2024
2.0.10 95 1/19/2024
2.0.9 77 1/18/2024
2.0.8 96 1/15/2024
2.0.7 138 11/27/2023
2.0.6 108 11/3/2023
2.0.5 108 10/27/2023
2.0.4 110 10/27/2023
2.0.3 113 10/27/2023
2.0.2 112 10/27/2023
2.0.1 123 10/25/2023
2.0.0 122 10/12/2023
1.1.3 133 9/27/2023
1.1.2 110 9/20/2023
1.1.1 128 9/13/2023
1.1.0 123 9/13/2023
1.0.15 118 9/12/2023
1.0.14 128 9/12/2023
1.0.13 130 9/8/2023
1.0.12 131 8/25/2023
1.0.11 125 8/11/2023
1.0.10 151 8/10/2023
1.0.9 138 8/9/2023
1.0.8 143 8/9/2023
1.0.7 147 8/9/2023
1.0.6 160 8/4/2023
1.0.5 152 8/3/2023
1.0.4 148 8/3/2023
1.0.3 155 8/3/2023
1.0.2 149 8/3/2023
1.0.1 158 8/2/2023
1.0.0 153 8/2/2023