CNative.Dapper.Utils
2.0.377
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
.NET Framework 4.0
This package targets .NET Framework 4.0. The package is compatible with this framework or higher.
dotnet add package CNative.Dapper.Utils --version 2.0.377
NuGet\Install-Package CNative.Dapper.Utils -Version 2.0.377
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="CNative.Dapper.Utils" Version="2.0.377" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CNative.Dapper.Utils --version 2.0.377
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CNative.Dapper.Utils, 2.0.377"
#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 CNative.Dapper.Utils as a Cake Addin #addin nuget:?package=CNative.Dapper.Utils&version=2.0.377 // Install CNative.Dapper.Utils as a Cake Tool #tool nuget:?package=CNative.Dapper.Utils&version=2.0.377
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CNative.Dapper.Utils
介绍
CNative.Dapper.DbUtils is the ORM in .NetCore, .NetFramework. It supports Mysql, SqlServer, Oracle,SqlLite,Npgsql,MsAccess
基于 Dapper 构建的微型 ORM 类库,提供一个包含增、删、改、查,分页等常用方法的数据访问层基类,支持用 Lambda 表达式书写查询和更新条件,且实体类有T4模版自动生成.省去手写实体类的麻烦。
软件架构
软件架构说明
Features
- 零配置,开箱即用。
- 数据库表、实体类型自动映射,主键自动映射。
- 灵活易用的增、删、改、查、分页查询等常用重载方法,单表操作无需编写任何 SQL 语句。
- 查询和更新条件支持 Lambda 表达式组合,自动生成安全参数化的 SQL 语句。
- 提供 SQL 语句、存储过程执行方法,返回结果集自动模型映射,比 DataSet 效率高。
- 支持部分字段更新,无变化字段不更新。
- 数据库表字段变化时重新生成实体类即可,数据访问层无需重新生成。
- 完善的单元测试。
- 支持多表联合查询
- 支持分页查询
- 支持主从库模式
- 支持环境: net40;net461;netstandard2.1;netcoreapp3.1;net5.0
- 目前支持Mysql, SqlServer, SqlServer2000,Oracle,SqlLite,Npgsql,MsAccess
- 动态库分别为 MySql.Data.dll,Oracle.ManagedDataAccess.dll,Devart.Data.Oracle.dll,System.Data.SqlClient.dll,System.Data.SQLite.dll,System.Data.OleDb.dll,Npgsql.dll
- 动态库无需引用,运行时会自动加载,和执行文件同一个目录就行
- 有丰富的测试用例
- 添加脚本跟踪功能
安装教程
MyGet Pre-release feed: https://www.nuget.org/packages/CNative.Dapper.Utils/
Package | NuGet Stable | NuGet Pre-release | Downloads | MyGet |
---|---|---|---|---|
CNative.Dapper.Utils.Utils |
使用说明
appsettings.json
{
"AppSettings": {
"Title": "Test",
"Version": "1.2.1",
"AccessToken": "xxxxxx@abc.com"
},
"connectionStrings": [
{
"name": "BaseDb",
"CommandTimeout": 45,//秒
"connectionString": "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=xxxxxx",
"providerName": "System.Data.SqlClient"
},
{
"name": "Oracle",
"connectionString": "Data Source=xxxxxx;Persist Security Info=True;User ID=hispro;Password=xxxxxx",
"providerName": "System.Data.OracleClient"
},
{
"name": "MySql",
"connectionString": "server=xxxxxx;uid=root;pwd=xxxxxx;database=test",
"providerName": "System.Data.MySql"
},
{
"name": "Sqlite",
"connectionString": "DataSource=xxxxxx.db;Version=3;Pooling=False;Max Pool Size=100;",
"providerName": "System.Data.Sqlite"
},
{
"name": "PostgreSql",
"connectionString": "User id=postgres;Password=xxxxxx;Host=xxxxxx;Port=55433;Database=test;Pooling=true;",
"providerName": "System.Data.Npgsql"
}{
"name": "MsAccess",
"connectionString": "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Documents\DevExpress 2010.1 Demos\Components\ASPxEditors\CS\ASPxEditorsDemos\App_Data\nwind.mdb;Persist Security Info=False ",
"providerName": "System.Data.Access"
},
{
"name": "MsAccess2",
"IsUseMasterSlaveSeparation": true,//是否启用主从分离模式
"SlaveConnectStrings": [ "MsAccess", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Documents\DevExpress 2010.1 Demos\Components\ASPxEditors\CS\ASPxEditorsDemos\App_Data\nwind.mdb;Persist Security Info=False " ],//从数据库连接字符串集合,
"connectionString": "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Documents\DevExpress 2010.1 Demos\Components\ASPxEditors\CS\ASPxEditorsDemos\App_Data\nwind.mdb;Persist Security Info=False",
"providerName": "CNative.Dapper.Utils.MsAccessProvider"
},{
"name": "OracleOleDbM2",
"connectionString": "Provider=MSDAORA;User Id=xxx;Password=xxxx;Persist Security Info=True;Data Source=(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxx)(PORT = 1521)))(CONNECT_DATA = (SID = orcl)))",
"providerName": "System.Data.OracleOleDb"
},
{
"name": "sql2000",
"connectionString": "Data Source=xxxxxxxx;Initial Catalog=ave2;User ID=sa;Password=xxxxxxx",
"providerName": "System.Data.SqlClient2000"
}
]
}
//测试用例
//脚本跟踪功能
SqlMapperTrace.SetMapperTrace((trec)=>
{
Console.WriteLine(trec);
}, (trec) =>
{
Console.WriteLine(trec);
});
[TestClass]
public class UnitTest461DbUtils
{
private ISqlBuilder DRY2 = null;
private ISqlBuilder sqlBuilder = null;
//在运行每个测试之前,使用 TestInitialize 来运行代码
[TestInitialize()]
public void MyTestInitialize()
{
DRY2 = new SqlBuilder("BaseDb");
sqlBuilder = new SqlBuilder("BaseDb");
}
[TestMethod]
//单表操作测试
[TestMethod]
public void TestSelectSqlBuilder1()
{
var lst = sqlBuilder.doSelect<Entity_persons>()//查询集合+排序+TOP
.Fields(s ⇒ new { s.id, s.name, s.adress })//添加查询多个字段
.Top(8)
// .Where(w ⇒ w.id.Between(10, 20))
.OrderByDescending(d ⇒ d.createTime)//按列倒向排序
.Query(); //返回结果
var lst3 = sqlBuilder.doSelect<Entity_persons>()//取单行+排序
.Fields(s ⇒ new { s.id, s.name, s.adress })//添加查询多个字段
.Where(w ⇒ w.id.Between(11, 20))
.OrderBy(d ⇒ d.id) //按列排序
.QuerySingle(); //返回结果
var lst4 = sqlBuilder.doSelect<Entity_persons>().Count(w ⇒ w.id.Between(11, 20)); //返回结果
}
[TestMethod]
public void TestInsertSqlBuilder1()
{
var jg = new Entity_DMJGXXB() { OrgId = 1002, jgdm = "56783", jgmc = "方舱" };
var ret = sqlBuilder.doDelete<Entity_DMJGXXB>()//实体删除
.Delete(jg)
.Exec;
ret = sqlBuilder.doInsert<Entity_DMJGXXB>()//插入实体
.Insert(jg)
.Execute();
ret = sqlBuilder.doDelete<Entity_DMJGXXB>()//表达式删除
.Where(s ⇒ s.OrgId == 1003)
.Execute();
ret = sqlBuilder.doInsert<Entity_DMJGXXB>()//表达式插入
.Insert(new Entity_DMJGXXB()
{
OrgId = 1003,
jgdm = "532236783",
jgmc = "方舱3",
CreateId = 0,
UpdateId = 0,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
})
.Exec;
//表复制
var ret1 = sqlBuilder.doDelete<Entity_DMCZYJGDYB2>()
.Where(s ⇒ s.ksid == 1100453)
.Execute();
var ret22 = sqlBuilder.doInsert<Entity_DMCZYJGDYB2>()//表复制
.InsertSelect<Entity_DMCZYJGDYB>(s ⇒ new Entity_DMCZYJGDYB2()
{
OrgId = s.OrgId,
czyid = s.czyid,
czyjgdyid = s.czyjgdyid,
czyxm = s.czyxm,
jgmc = "fasdfafdsaf",
ksid = s.ksid,
ksmc = s.ksmc
}, s ⇒ s.ksid == 1100453)
.Exec;
}
[TestMethod]
public void TestUpdateSqlBuilder1()
{
var ret = sqlBuilder.doUpdate<Entity_DMJGXXB>()
.Set(s ⇒ new Entity_DMJGXXB()
{
jgdm = "5322336783",
jgmc = "fff232aaa",
CreateId = 0,
UpdateId = 0,
jgjpm = "ffff",
UpdateTime = DateTime.Now
})
.Where(s ⇒ s.OrgId == jg.OrgId)
.Exec;
jg.jgjpm = "345454";
ret = sqlBuilder.DoUpdate
.Update(jg, "jgjpm");
var ret1 = sqlBuilder.doDelete<Entity_DMJGXXB>()
.Where(s ⇒ s.OrgId == 1005)
.Execute();
//批量导入数据
ret = sqlBuilder.doInsert<Entity_DMJGXXB>()
.BulkCopyData(new List<Entity_DMJGXXB>(){new Entity_DMJGXXB()
{
OrgId = 1004,
jgdm = "532236784",
jgmc = "方舱4",
CreateId = 0,
UpdateId = 0,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
},new Entity_DMJGXXB()
{
OrgId = 1005,
jgdm = "532236785",
jgmc = "方舱5",
CreateId = 0,
UpdateId = 0,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
},new Entity_DMJGXXB()
{
OrgId = 1006,
jgdm = "532236786",
jgmc = "方舱6",
CreateId = 0,
UpdateId = 0,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
} });
//更新或插入
var ret23 = sqlBuilder.DoUpdate
.UpdateOrInsert(jg, w ⇒ w.OrgId == jg.OrgId);
ret = sqlBuilder.doUpdate<Entity_DMJGXXB>()
.UpdateOrInsert(s ⇒ new Entity_DMJGXXB()
{
OrgId = 1006,
jgdm = "5322336783",
jgmc = "方舱232",
CreateId = 0,
UpdateId = 0,
jgjpm = "wsafdasfd",
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
}, w ⇒ w.OrgId == 1006);
}
//多表操作测试
[TestMethod]
public void TestSelectFieldExprSqlBuilder1()
{
//查寻时加集合函数
var ret1 = sqlBuilder.doSelect<Entity_DMCZYJGDYB2>()
.Fields(f ⇒ new { czyjgdyid = f.czyjgdyid.SQL_MAX() })
.Where(s ⇒ s.ksid == 1100453)
.GetSingle<object>();
//查寻返回单行实体
var ret2 = sqlBuilder.doSelect<Entity_DMJGXXB>()
.Where(s => s.OrgId == 1008)
.QuerySingle();
var ret2 = sqlBuilder.doSelect<Entity_DMCZYJGDYB2>()
.Fields(f ⇒ new
{ f.czyid, f.czyxm, jgmc = f.jgmc.SQL_UCASE(), ksmc = f.jgmc.SQL_SUBSTR(2, 3) })
//.Where(s ⇒ s.ksid == 1100453)
.Where(s ⇒ s.ksid == 1100453 && s.jgmc.SQL_UCASE() == "FASDFAFDSAF")
.Query();
var ret3 = sqlBuilder.doSelect<Entity_DMCZYJGDYB2>()
.Fields(f ⇒ new { f.ksid, f.ksmc, czyjgdyid = f.czyjgdyid.SQL_MAX() })
.Where(s ⇒ s.OrgId == 1001)
.GroupBy(g ⇒ new { g.ksid, g.ksmc })
.OrderBy(g ⇒ g.ksid)
.Query();
var ret4 = sqlBuilder.doSelect<Entity_DMCZYJGDYB2>()
.Fields(f ⇒ new { f.ksid, f.ksmc, czyjgdyid = f.czyjgdyid.SQL_COUNT(), czyid = f.czyjgdyid.SQL_MAX() })
.Where(s ⇒ s.OrgId == 1001)
.GroupBy(g ⇒ new { g.ksid, g.ksmc })
.Having(h ⇒ h.czyjgdyid.SQL_COUNT() > 3 && h.czyjgdyid.SQL_MAX() > 0)
.OrderBy(g ⇒ g.ksid)
.Query();
}
特技
- 如果其中的代码有不妥的地方,欢迎各位大佬进行指正! 非常感谢!!!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. 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 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 | netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 is compatible. 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 | 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.
-
.NETCoreApp 3.1
- CNative.Utilities (>= 2.1.14)
- Dapper (>= 2.0.123)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Data.OleDb (>= 5.0.0)
-
.NETFramework 4.0
- CNative.Utilities (>= 2.1.14)
- Dapper (>= 1.50.1)
-
.NETFramework 4.6.1
- CNative.Utilities (>= 2.1.14)
- Dapper (>= 2.0.123)
- System.ComponentModel.Annotations (>= 5.0.0)
-
.NETStandard 2.1
- CNative.Utilities (>= 2.1.14)
- Dapper (>= 2.0.123)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Data.OleDb (>= 5.0.0)
-
net5.0
- CNative.Utilities (>= 2.1.14)
- Dapper (>= 2.0.123)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Data.OleDb (>= 5.0.0)
-
net6.0
- CNative.Utilities (>= 2.1.14)
- Dapper (>= 2.0.123)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Data.OleDb (>= 6.0.0)
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 |
---|---|---|
2.0.377 | 646 | 5/13/2022 |
2.0.375 | 437 | 3/27/2022 |
2.0.374 | 427 | 3/26/2022 |
2.0.373 | 440 | 3/25/2022 |
2.0.372 | 424 | 3/25/2022 |
2.0.371 | 419 | 3/17/2022 |
2.0.37 | 471 | 3/17/2022 |
2.0.35 | 441 | 3/14/2022 |
2.0.34 | 459 | 3/11/2022 |
2.0.33 | 430 | 3/11/2022 |
2.0.32 | 449 | 3/10/2022 |
2.0.31 | 416 | 3/10/2022 |
2.0.30 | 437 | 2/10/2022 |
2.0.29 | 451 | 1/17/2022 |
2.0.28 | 427 | 1/17/2022 |
2.0.27 | 280 | 12/14/2021 |
2.0.26-gc5b80360e1 | 221 | 12/13/2021 |
2.0.25-gc3ae86d0a4 | 188 | 12/13/2021 |
2.0.24-g3c6a0dfbba | 209 | 12/13/2021 |
2.0.23-g73c97e6424 | 224 | 12/10/2021 |
2.0.21-g4da30d5c1e | 244 | 9/26/2021 |
2.0.20-g0174deb0b5 | 244 | 7/22/2021 |
2.0.14-gacd961bcfc | 281 | 7/21/2021 |
2.0.12-gf73eb78747 | 267 | 7/21/2021 |
2.0.5-g062d5c3918 | 234 | 7/21/2021 |
2.0.1-g46b6b60978 | 242 | 7/21/2021 |