Aohai.Tools.CRUD
1.0.0
dotnet add package Aohai.Tools.CRUD --version 1.0.0
NuGet\Install-Package Aohai.Tools.CRUD -Version 1.0.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="Aohai.Tools.CRUD" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aohai.Tools.CRUD" Version="1.0.0" />
<PackageReference Include="Aohai.Tools.CRUD" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aohai.Tools.CRUD --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Aohai.Tools.CRUD, 1.0.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.
#:package Aohai.Tools.CRUD@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Aohai.Tools.CRUD&version=1.0.0
#tool nuget:?package=Aohai.Tools.CRUD&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DBHelper - 简化 SQL Server 数据库操作
DBHelper 是一个轻量级的 .NET 库,旨在简化与 SQL Server 数据库的交互。它提供了一组简单易用的方法来执行常见的数据库操作,如 SELECT、UPDATE、INSERT 和 DELETE,并支持参数化查询和泛型结果映射。
主要特性
- 简单易用: 只需传入连接字符串和 SQL 语句,即可执行数据库操作。
- 参数化查询: 支持使用
SqlParameter对象进行参数化查询,有效防止 SQL 注入攻击。 - 泛型支持: 可以将查询结果直接映射到自定义的模型类 (Model)。
- JSON 结果: 查询结果以
JArray格式返回,方便处理和序列化。 - 异常处理: 内置
try-catch块,捕获并处理数据库操作可能出现的异常。 - 支持对象操作: 支持传入对象新增和更新操作。
安装
通过 NuGet 包管理器安装: Use code with caution. Markdown Install-Package YourPackageName
或者 Use code with caution. dotnet add package YourPackageName
将 YourPackageName 替换为你实际的 NuGet 包名称。
快速入门
在使用前,请先安装以下Nuget包 Use code with caution. Install-Package Microsoft.Data.SqlClient Install-Package Newtonsoft.Json
using Microsoft.Data.SqlClient;
using Newtonsoft.Json.Linq;
// 创建 DBHelper 实例
string connectionString = "Your_Connection_String";
string tableName = "Your_Table_Name";
DBHelper dbHelper = new DBHelper(connectionString, tableName);
// 执行查询 (非泛型)
JArray result = dbHelper.Find("SELECT * FROM your_table_name WHERE Id = @Id", new SqlParameter("@Id", 1));
// 执行查询 (泛型)
public class MyModel
{
public int Id { get; set; }
public string Name { get; set; }
}
List<MyModel> models = dbHelper.Find<MyModel>("SELECT * FROM YourTable");
// 执行 INSERT
int rowsAffected = dbHelper.Insert("INSERT INTO your_table_name (Name) VALUES (@Name)", new SqlParameter("@Name", "New Value"));
//执行Update
int rowsAffected = dbHelper.Update("UPDATE your_table_name SET Name = @Name WHERE Id = @Id",
new SqlParameter("@Name", "更新后的值"),
new SqlParameter("@Id", 1));
// 执行 DELETE
int rowsAffected = dbHelper.Delete("DELETE FROM your_table_name WHERE Id = @Id", new SqlParameter("@Id", 1));
//传入实体对象插入
MyModel newModel = new MyModel { Name = "New Record" };
int rowsAffected = dbHelper.Insert(newModel);
//传入实体对象更新
MyModel updateModel = new MyModel { Id = 1, Name = "Updated Name" };
int rowsAffected = dbHelper.Update(updateModel, "Id = @Id", new SqlParameter("@Id", 1));
Use code with caution.
API 说明
DBHelper 类
构造函数
/// <summary>
/// 初始化 DBHelper 类的新实例。
/// </summary>
/// <param name="connectionString">数据库连接字符串。</param>
/// <param name="tableName">表名称</param>
public DBHelper(string connectionString,string tableName)
Use code with caution.
C#
方法
Find(string sql, params SqlParameter[] parameters):
/// <summary>
/// 执行查询操作,并返回JArray
/// </summary>
/// <param name="sql">要执行的 SQL 查询语句。</param>
/// <param name="parameters">可选的 SQL 参数数组。</param>
/// <returns>包含查询结果的 JArray。</returns>
public JArray Find(string sql, params SqlParameter[] parameters)
Use code with caution.
C#
Find<T>(string sql, params SqlParameter[] parameters) where T : new():
/// <summary>
/// 执行查询操作,并将结果映射到指定类型的对象列表。
/// </summary>
/// <typeparam name="T">要映射到的目标类型。</typeparam>
/// <param name="sql">要执行的 SQL 查询语句。</param>
/// <param name="parameters">可选的 SQL 参数数组。</param>
/// <returns>包含查询结果的 List<T>。</returns>
public List<T> Find<T>(string sql, params SqlParameter[] parameters) where T : new()
Use code with caution.
C#
Insert(string sql, params SqlParameter[] parameters):
/// <summary>
/// 执行增操作,并返回影响行数
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public int Insert(string sql, params SqlParameter[] parameters)
Use code with caution.
C#
Insert<T>(T model):
/// <summary>
/// 新增根据Model插入数据的方法,传入实体对象插入
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="model">实体对象</param>
/// <returns></returns>
public int Insert<T>(T model)
Use code with caution.
C#
Update(string sql, params SqlParameter[] parameters):
/// <summary>
/// 执行改操作,并返回影响行数
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public int Update(string sql, params SqlParameter[] parameters)
Use code with caution.
C#
Update<T>(T model, string whereSql, params SqlParameter[] whereParameters):
/// <summary>
/// 新增根据Model更新数据的方法,传入实体对象更新
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="model">实体</param>
/// <param name="whereSql">条件</param>
/// <param name="whereParameters">条件参数</param>
/// <returns></returns>
public int Update<T>(T model, string whereSql, params SqlParameter[] whereParameters)
Use code with caution.
C#
Delete(string sql, params SqlParameter[] parameters):
/// <summary>
/// 执行删操作,并返回影响行数
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public int Delete(string sql, params SqlParameter[] parameters)
Use code with caution.
C#
异常处理
所有数据库操作都在 try-catch 块中执行。如果发生 SqlException 或其他异常,将会在控制台输出错误信息,并根据具体情况决定是抛出异常、返回 null 还是空的 JArray。 你可以根据需要在调用代码中进一步处理这些异常。
依赖项
Microsoft.Data.SqlClient
Newtonsoft.Json
贡献
欢迎提交问题报告和拉取请求。
作者
aohai
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.Data.SqlClient (>= 6.0.1)
- Newtonsoft.Json (>= 13.0.3)
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.0 | 243 | 2/25/2025 |