TA.DataAccess.SqlServer 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package TA.DataAccess.SqlServer --version 1.0.5                
NuGet\Install-Package TA.DataAccess.SqlServer -Version 1.0.5                
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="TA.DataAccess.SqlServer" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TA.DataAccess.SqlServer --version 1.0.5                
#r "nuget: TA.DataAccess.SqlServer, 1.0.5"                
#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 TA.DataAccess.SqlServer as a Cake Addin
#addin nuget:?package=TA.DataAccess.SqlServer&version=1.0.5

// Install TA.DataAccess.SqlServer as a Cake Tool
#tool nuget:?package=TA.DataAccess.SqlServer&version=1.0.5                

TA.DataAccess.SqlServer

TA.DataAccess.SqlServer is a comprehensive .NET library designed to simplify database operations with SQL Server. It provides a range of methods to execute queries, retrieve data, and perform CRUD operations using strongly-typed models.

Features

  • Execute non-query commands
  • Execute multiple non-query commands within a transaction
  • Retrieve data as DataTable
  • Retrieve data as a list of strongly-typed models
  • Insert, update, and delete models
  • Use custom attributes to exclude properties from CRUD operations

Installation

You can install the package via NuGet Package Manager:

dotnet add package TA.DataAccess.SqlServer

PM> NuGet\Install-Package TA.DataAccess.SqlServer

Usage

Configuration

Ensure your appsettings.json has the connection string:

{
  "ConnectionStrings": {
    "DefaultConnection": "Your Connection String Here"
  }
}

Dependency Injection

Register the SqlServerHelper in your Startup.cs or Program.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ISqlServerHelper, SqlServerHelper>();  
}

Example Usage

using Microsoft.Extensions.Configuration;
using TA.DataAccess.SqlServer;
using System.Collections.Generic;
using System.Data;

public class MyService
{
    private readonly ISqlServerHelper _sqlServerHelper;

    public MyService(ISqlServerHelper sqlServerHelper)
    {
        _sqlServerHelper = sqlServerHelper;
    }

    public void ExecuteSampleQueries()
    {
            // Test ExecuteNonQuery with a single query
            string createTableQuery = "CREATE TABLE TestTable (Id INT PRIMARY KEY, Name NVARCHAR(50))";
            _sqlServerHelper.ExecuteNonQuery(createTableQuery);

            // Test ExecuteNonQuery with multiple queries
            var queries = new List<string>
            {
                "INSERT INTO TestTable (Id, Name) VALUES (1, 'Test Name 1')",
                "INSERT INTO TestTable (Id, Name) VALUES (2, 'Test Name 2')"
            };
            _sqlServerHelper.ExecuteNonQuery(queries);

            // Test Select with a single query
            string selectQuery = "SELECT * FROM TestTable";
            var dataTable = _sqlServerHelper.Select(selectQuery);

            // Test Select<T> with a single query
            var results = _sqlServerHelper.Select<TestModel>("SELECT * FROM TestTable");
            

            // Test InsertModel
            var newModel = new TestModel { Id = 3, Name = "Test Name 3" };
            _sqlServerHelper.InsertModel(newModel, "TestTable");

            // Test InsertModels
            var newModels = new List<TestModel>
            {
                new TestModel { Id = 4, Name = "Test Name 4" },
                new TestModel { Id = 5, Name = "Test Name 5" }
            };
            _sqlServerHelper.InsertModels(newModels, "TestTable");

            // Test GetAllModels
            var allModels = _sqlServerHelper.GetAllModels<TestModel>("TestTable");

            // Test GetModelById
            var modelById = _sqlServerHelper.GetModelById<TestModel>("TestTable", "Id", 1);

            // Test UpdateModel
            modelById.Name = "Updated Test Name 1";
            _sqlServerHelper.UpdateModel(modelById, "TestTable", "Id");

            // Test DeleteModel
            _sqlServerHelper.DeleteModel("TestTable", "Id", 1);

            // Clean up
            _sqlServerHelper.ExecuteNonQuery("DROP TABLE TestTable");
    }
}


public class TestModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Custom Attributes

You can use the NoCrudAttribute to exclude properties from CRUD operations:

public class TestModel
{
    public int Id { get; set; }
    
    [NoCrud]
    public string ExcludedColumn { get; set; }
    
    public string Name { get; set; }
}

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 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. 
.NET Core netcoreapp3.1 is compatible. 
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.0.6 119 6/10/2024
1.0.5 90 6/7/2024
1.0.4 107 6/7/2024
1.0.3 112 6/6/2024
1.0.2 110 6/5/2024
1.0.1 114 6/5/2024
1.0.0 116 6/5/2024

Bug Fixed