Skip to content

Latest commit

 

History

71 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UkrGuru.Sql

Nuget Donate

UkrGuru.Sql is a powerful library designed to simplify interactions between .NET applications and SQL Server databases. It automatically normalizes input parameters and deserializes results, supporting dynamic queries, stored procedures, and asynchronous operations. With UkrGuru.Sql, you can access SQL Server data with minimal code and maximum performance.

Features

  • Easy Integration: Seamlessly integrate with ASP.NET Core projects.
  • Dynamic Queries: Support for dynamic SQL queries and stored procedures.
  • Asynchronous Operations: Built-in support for async operations.
  • Parameter Normalization: Automatic normalization of input parameters.
  • Result Deserialization: Effortless deserialization of query results.

Installation

To use the UkrGuru.Sql library in your ASP.NET Core project, follow these steps:

  1. Install the UkrGuru.Sql package from NuGet:
dotnet add package UkrGuru.Sql
  1. Update your appsettings.json file to include the connection string:
{
    "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDbName;Trusted_Connection=True;"
    }
}
  1. Register the UkrGuru.Sql services in your Program.cs file:
using UkrGuru.Sql;
   
// Set connection string for static use
DbHelper.ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");

var builder = WebApplication.CreateBuilder(args);

// Register DbService for DI
builder.Services.AddScoped<IDbService>(sp =>
    new DbService(builder.Configuration.GetConnectionString("DefaultConnection")!)
);
      
var app = builder.Build();

Samples

Code Breakdown and Corresponding Results

  1. Addition Query

    var result = DbHelper.Exec<int>("SELECT @A + @B", new { A = 2, B = 2 });
    Console.WriteLine($"Result: {result}");
    • Output: Result: 4
  2. String Retrieval from Anonymous Object

    var person = new { Id = 1, Name = "John" };
    var name = DbHelper.Exec<string>("SELECT @Name", person);
    Console.WriteLine($"\r\nResult: {name}");
    • Output: Result: John
  3. Reading Multiple Records

    var persons = DbHelper.Read<Person>("""
         SELECT 1 Id, 'John' Name
         UNION ALL SELECT 2, 'Mike'
         """).ToList();
    Console.WriteLine($"\r\nResult: {persons.Count}");
    Console.WriteLine($"1st person: {persons[0].ToJson()}");
    Console.WriteLine($"2nd person: {persons[1].ToJson()}");
    • Output:
    Result: 2
    1st person: {"Id":1,"Name":"John"}
    2nd person: {"Id":2,"Name":"Mike"}
    

You can find more examples in the demos folder of this repository.

Contributing

Contributions are welcome! Please fork this repository and submit pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

UkrGuru.Sql is a powerful library designed to simplify interactions between .NET applications and SQL Server databases.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages