Electronicute.Meow.DataBase 6.0.0

dotnet add package Electronicute.Meow.DataBase --version 6.0.0
NuGet\Install-Package Electronicute.Meow.DataBase -Version 6.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="Electronicute.Meow.DataBase" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Electronicute.Meow.DataBase --version 6.0.0
#r "nuget: Electronicute.Meow.DataBase, 6.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.
// Install Electronicute.Meow.DataBase as a Cake Addin
#addin nuget:?package=Electronicute.Meow.DataBase&version=6.0.0

// Install Electronicute.Meow.DataBase as a Cake Tool
#tool nuget:?package=Electronicute.Meow.DataBase&version=6.0.0

Meow Database

alternate text is missing from this package README image MeowDataBase

1. 引 言

1.1 总

本包当前为Mysql.Data(Nuget)标准包的二次封装, 其目的是为了更加便捷的简写使用.

1.2 包和包更新

您可以在 Nuget 搜索 Electronicute.Meow.DataBase
如果在Nuget更新您只需要关注VisualStudio的内部的Dep管理即可
如果为单独下载或者Clone本库请注意版本和时间

1.3 包的计划内容

  1. 更新关于DataTable绑定备份和云端快速缓存类的使用
  2. 根据微软语法糖提供更多简写方案

1. 本次更新内容(20231030)

  1. 降级到.netStandard 2.0

1. 本次更新内容(20221004)

  1. SelectExist()函数, 用于检查选取域是否含有行.
  2. GetRows()函数, 用于直接获取行集合.
  3. GetFirstRowItem()函数, 用于直接获取一行(当选择域只有一行的时候)的某一列的某个单元.

2. 包特性

2.1 命名空间

本包的引用起始命名空间为 Meow. 而非 Electronicute.

2.2 包含状况

本包亦被 Electronicute.Meow 包含但其提供更多API,如果您无需使用太多API建议单独下载本包即可

2.3 连写和连写处理

您可以使用连写特性(语法糖)进行快速开发.详细信息请参见使用方案


3. 使用方案

3.1 建立数据库链接/生成DBHelper

3.1.1 使用实例化链接方案
using MysqlDBH dblk = new("dbname", "ip", "port", "user", "pass");
using SQLiteDBH sqlite = new("D:\\testdb.db");
3.1.2 使用链接字符串
using SQLiteDBH sqlite = new("D:\\testdb.db");
using MysqlDBH dblk = new($"Database={DataBase};DataSource={DataSource};Port={Port};UserId={UserId};Password={password};Charset={Charset};{otherParameter}");

注1: 使用 using 引起是因为 MysqlDBHSQLiteDBH 实现了接口 IDisposable,
从而在其生命周期内(定义域/作用域内)当其失去使用效果时自动断开链接,
防止过多链接导致Mysql达到最大连接数;

注2: 如果您希望一直保持Mysql的单一(长)链接链接数据库, 您需要实现如下功能:
1.将实例化的DBH设置为全局静态成员
2.设置参数 initOpentrue
3.设置参数 KeepAlivetrue (默认设置false)
4.设置参数 MaxTimedOut为您的数据库默认最大值之内 (默认设置 600000ms (10分钟))
否则Mysql主机将会主动断开超过8小时未进行任何操作的链接,导致程序出错.

注3: 并不建议提高Mysql数据库对于超时链接的默认设定.

3.2 准备数据库操作并返回[函数:PrepareDb]
using var d = ReturnService(); //数据库链接已经准备完毕
3.2.1 增删改查,修改权限等 (PlainText)

总写法: HelperInstance.PrepareDb(...).Operation();

3.2.1.1 无需参数和变量传入的操作
//查表,并返回整个表
var kx = d.PrepareDb("SELECT * FROM table").GetTable();
//获取表内某行元素
var colname = "col";
var row = 0;
if (kx.Rows.Count > 0)
{
    return kx.Rows[row].Field<int>(colname); //有行(并且获取一个Int值)
}
else
{
    return 0; //空行
}

注 1. 因为SQLite的支持类型有限, 您在引用 Meow.DataBase 命名空间后,
可以使用经由SQLiteDataHelper静态类实现的扩展方法
扩展方法如下: GetInt GetString GetFloat GetBLOB Get<T>
您可以简写获取元素为如下逻辑模式:

var vk = tbfo.Rows[0].GetInt("name");
//执行NonQuery类型
var kx = d.PrepareDb("INSERT INTO table (col1,col2,col3) VALUES (1,2,3)").ExecuteNonQuery();
//检测是否成功
var roweffect = kx;
return roweffect>0;

//简写方案(最佳实践)
return d.PrepareDb("INSERT INTO table (col1,col2,col3) VALUES (1,2,3)").ExecuteNonQuery()>0;
3.2.1.2 需要传入变量的操作
///无限添加参数 (param方案)
int c = 0;
var kx = d.PrepareDb("SELECT c FROM table WHERE c1=@c1 and c2=@c2 ORDER BY c DESC LIMIT 1",
		new MySql.Data.MySqlClient.MySqlParameter("@c1", (int)c),
		new MySql.Data.MySqlClient.MySqlParameter("@c2", (int)c) //.....
		).GetTable();
///无限添加参数方案二 (数组方案)
int c = 0;
var kx2 = d.PrepareDb("SELECT c FROM table WHERE c1=@c1 and c2=@c2 ORDER BY c DESC LIMIT 1",
        new MySql.Data.MySqlClient.MySqlParameter[] {new("@c1",c),new("@c2",c)}
        ).GetDataSet().Tables[0];

4.附录

4.1 返回值

返回值均为 System.Data 内部的合成值,包含了 System.Data 的最佳实现,

4.2 内函数实现

4.2.1 SelectExist
public bool SelectExist() => GetTable().Rows.Count > 0;
4.2.2 GetRows
public DataRowCollection GetRows() => GetTable()?.Rows;
4.2.2 GetFirstRowItem<T>
public T GetFirstRowItem<T>(string colname) => GetTable().Rows[0].Field<T>(colname);

4.3 可不可以进行字符串内插来更改参数

可以,但不建议,因为你需要手动过滤并且防范Sql注入攻击,
其实使用SqlPara也未必能完全防御但 无论是从写法还是逻辑安全性他都要比字符串内插高.

4.4 事务等其他的语句怎么提交

使用基础语句

.....
PrepareDb("statement", CommandType.StoredProcedure //更改此处
.....	
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
6.0.0 628 10/30/2023
5.2.3 850 1/24/2023