TA.DataAccess.SqlServer
1.0.4
See the version list below for details.
dotnet add package TA.DataAccess.SqlServer --version 1.0.4
NuGet\Install-Package TA.DataAccess.SqlServer -Version 1.0.4
<PackageReference Include="TA.DataAccess.SqlServer" Version="1.0.4" />
paket add TA.DataAccess.SqlServer --version 1.0.4
#r "nuget: TA.DataAccess.SqlServer, 1.0.4"
// Install TA.DataAccess.SqlServer as a Cake Addin #addin nuget:?package=TA.DataAccess.SqlServer&version=1.0.4 // Install TA.DataAccess.SqlServer as a Cake Tool #tool nuget:?package=TA.DataAccess.SqlServer&version=1.0.4
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 | Versions 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. |
-
.NETCoreApp 3.1
- Microsoft.Data.SqlClient (>= 5.2.1)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- System.Data.SqlClient (>= 4.8.6)
-
net5.0
- Microsoft.Data.SqlClient (>= 5.2.1)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- System.Data.SqlClient (>= 4.8.6)
-
net8.0
- Microsoft.Data.SqlClient (>= 5.2.1)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- System.Data.SqlClient (>= 4.8.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Bug Fixed