zijian666.DatabaseClientExtensions 1.3.0-beta

This is a prerelease version of zijian666.DatabaseClientExtensions.
dotnet add package zijian666.DatabaseClientExtensions --version 1.3.0-beta                
NuGet\Install-Package zijian666.DatabaseClientExtensions -Version 1.3.0-beta                
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="zijian666.DatabaseClientExtensions" Version="1.3.0-beta" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add zijian666.DatabaseClientExtensions --version 1.3.0-beta                
#r "nuget: zijian666.DatabaseClientExtensions, 1.3.0-beta"                
#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 zijian666.DatabaseClientExtensions as a Cake Addin
#addin nuget:?package=zijian666.DatabaseClientExtensions&version=1.3.0-beta&prerelease

// Install zijian666.DatabaseClientExtensions as a Cake Tool
#tool nuget:?package=zijian666.DatabaseClientExtensions&version=1.3.0-beta&prerelease                

zijian666.DatabaseClientExtensions

数据库拓展方法

介绍

很简单的demo,几乎所有方法都是从IDbConnectionIDbCommand标准类型扩展而来。
以最优雅,最便捷的方式操作数据库。 利用.NET扩展方法的语法糖,扩展.NET原生对象; 最大程度的避免对原有代码的污染,也方便与其他第三方组件库联动。

更新日志

点击查看

安装教程

nuget - zijian666.DatabaseClientExtensions

使用说明

利用FormattableString类型的字符串格式化特性,优化的传入参数
除了返回值自动转换泛型实体类之外,还可以返回动态类型(dynamic),操作更便捷

using (var conn = new MySqlConnection("Server=*;Port=*;Database=*;Uid=*;Pwd=*;"))
{
    var list = conn.ExecuteList<Students>($"select * from students");
    var first = conn.ExecuteFirst($"select * from students where id > {13} order by id");
    Console.WriteLine($"Name:{first.Name}");
}
支持数组参数

所有格式化字符串SQL都会被转为参数化执行,例如:
$"select * from x where id = {11}"
--> select * from x where id = @p1;
而数组转换会被转换为:
$"select * from x where id in ({new [] {1,2,3,4}})"
--> select * from x where id in (@p1,@p2,@p3,@p4)

最大返回行数

默认执行任何查询最多返回10万行, 除非手动指定行数

/// <summary>
/// 查询列表时的默认的最大返回行数
/// </summary>
public const int DEFAULT_LIMIT = 100000;
/// <summary>
/// 执行sql, 返回实体类集合
/// </summary>
/// <param name="limit">最大返回行数</param>
/// <returns></returns>
public static List<T> ExecuteList<T>(this IDbConnection conn, FormattableString sql, int limit = DEFAULT_LIMIT)
自动取消执行

当执行操作结束, 但DataReader中仍有数据为读出时, 自动调用DBCommand.Cancel()取消执行

public void Dispose()
{
    if (_reader.Read() || _reader.NextResult())
    {
        // 如果datareader未读完直接dispose,将会自动读取剩下的数据后才释放
        _command?.Cancel();
    }
    _reader?.Close();
    _reader?.Dispose();
    _reader = null;
}
非参数化变量
using (var conn = new MySqlConnection("Server=*;Port=*;Database=*;Uid=*;Pwd=*;"))
{
    var list = conn.ExecuteList<Students>($"select * from {table:!} limit 1");
}

新特性规划(未实现)

动态where
using (var conn = new MySqlConnection("Server=*;Port=*;Database=*;Uid=*;Pwd=*;"))
{
    var where = conn.CreateWhere();
    if (name != null)
    {
        where += $"Name = {name}";
    }
    if (age != 0)
    {
        where += $"Age > {age}"
    }
    var list = conn.ExecuteList<Students>($"select * from students {where}");
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 was computed.  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. 
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.3.0-beta 40 12/24/2024
1.1.2-beta 125 7/14/2023
1.1.1-beta 90 7/13/2023
1.0.11-beta 151 4/13/2022
1.0.9-beta 144 4/3/2022
1.0.3-beta 170 2/18/2021
1.0.2-beta 367 10/31/2020

UPLOGS.md