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
<PackageReference Include="Electronicute.Meow.DataBase" Version="6.0.0" />
paket add Electronicute.Meow.DataBase --version 6.0.0
#r "nuget: Electronicute.Meow.DataBase, 6.0.0"
// 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
1. 引 言
1.1 总
本包当前为Mysql.Data(Nuget)标准包的二次封装, 其目的是为了更加便捷的简写使用.
1.2 包和包更新
您可以在 Nuget
搜索 Electronicute.Meow.DataBase
如果在Nuget更新您只需要关注VisualStudio的内部的Dep管理即可
如果为单独下载或者Clone本库请注意版本和时间
1.3 包的计划内容
- 更新关于DataTable绑定备份和云端快速缓存类的使用
- 根据微软语法糖提供更多简写方案
1. 本次更新内容(20231030)
- 降级到.netStandard 2.0
1. 本次更新内容(20221004)
- SelectExist()函数, 用于检查选取域是否含有行.
- GetRows()函数, 用于直接获取行集合.
- 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
引起是因为 MysqlDBH
和 SQLiteDBH
实现了接口 IDisposable
,
从而在其生命周期内(定义域/作用域内)当其失去使用效果时自动断开链接
,
防止过多链接导致Mysql达到最大连接数;
注2: 如果您希望一直保持Mysql的单一(长)链接
链接数据库, 您需要实现如下功能:
1.将实例化的DBH设置为全局静态成员
2.设置参数 initOpen
为true
3.设置参数 KeepAlive
为true
(默认设置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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Data.Sqlite (>= 6.0.8)
- MySql.Data (>= 8.0.28)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.