TA.DataAccess.SqlServer
1.0.1
See the version list below for details.
dotnet add package TA.DataAccess.SqlServer --version 1.0.1
NuGet\Install-Package TA.DataAccess.SqlServer -Version 1.0.1
<PackageReference Include="TA.DataAccess.SqlServer" Version="1.0.1" />
paket add TA.DataAccess.SqlServer --version 1.0.1
#r "nuget: TA.DataAccess.SqlServer, 1.0.1"
// Install TA.DataAccess.SqlServer as a Cake Addin #addin nuget:?package=TA.DataAccess.SqlServer&version=1.0.1 // Install TA.DataAccess.SqlServer as a Cake Tool #tool nuget:?package=TA.DataAccess.SqlServer&version=1.0.1
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
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()
{
// Execute a non-query command
_sqlServerHelper.ExecuteNonQuery("INSERT INTO MyTable (Column1) VALUES (@Value)", new SqlParameter[] { new SqlParameter("@Value", "SampleValue") });
// Execute a query and get results as DataTable
DataTable dt = _sqlServerHelper.Select("SELECT * FROM MyTable");
// Execute a query and get results as a list of strongly-typed models
List<MyModel> models = _sqlServerHelper.Select<MyModel>("SELECT * FROM MyTable");
// Insert a model
MyModel model = new MyModel { Column1 = "Value1", Column2 = "Value2" };
_sqlServerHelper.InsertModel(model, "MyTable");
// Insert multiple models
List<MyModel> modelList = new List<MyModel> { model, model };
_sqlServerHelper.InsertModels(modelList, "MyTable");
}
}
public class MyModel
{
public int Id { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
}
Custom Attributes
You can use the NoCrudAttribute to exclude properties from CRUD operations:
public class MyModel
{
public int Id { get; set; }
[NoCrud]
public string ExcludedColumn { get; set; }
public string Column1 { get; set; }
public string Column2 { 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 | 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. |
-
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 and some basic info added