DbCore 2.2.0

dotnet add package DbCore --version 2.2.0
                    
NuGet\Install-Package DbCore -Version 2.2.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="DbCore" Version="2.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DbCore" Version="2.2.0" />
                    
Directory.Packages.props
<PackageReference Include="DbCore" />
                    
Project file
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 DbCore --version 2.2.0
                    
#r "nuget: DbCore, 2.2.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 DbCore@2.2.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=DbCore&version=2.2.0
                    
Install as a Cake Addin
#tool nuget:?package=DbCore&version=2.2.0
                    
Install as a Cake Tool

DbCore

DbCore是一个高性能线程安全的数据库持久层管理器,基于.Net Core(并不局限于此)。

设计DbCore的目的是回归简单,如果你习惯于编写SQL而不是ORM映射,DbCore是非常好的选择, 对于遵循微软的DbProviderFactory设计的驱动,DbCore可以管理各种类型的数据库,包括: Access、SQLite、SqlServer、MySql、Oracle、Firebird、PostgreSQL、DB2、Sybase ...

DbCore并没有完全拒绝ORM,并支持Linq,其应用逻辑是:直接编写SQL语句从数据库查询数据, 缓存到本地以后作为内存数据源应用Linq进行查询。至于采用实体对表进行增删改操作目前不再考虑范围之内, 你仍然需要编写SQL语句对表进行增删改。

DbCore的使用极其简单,只需三步,使用步骤如下:

第一步:下载数据库驱动程序,将dll及其依赖项放在Providers文件夹中,下面是各种数据库驱动下载:

类型:        SQLite
提供程序名称: System.Data.SQLite
项目位置:    https://system.data.sqlite.org/
下载位置:    https://www.nuget.org/packages/System.Data.SQLite.Core/

类型:        SqlServer
提供程序名称: System.Data.SqlClient
项目位置:    https://docs.microsoft.com/zh-cn/ef/core/
下载位置:    https://www.nuget.org/packages/System.Data.SqlClient/

类型:        MySql
提供程序名称: MySql.Data.MySqlClient
项目位置:    https://dev.mysql.com/downloads/
下载位置:    https://www.nuget.org/packages/MySql.Data/

类型:        Oracle
提供程序名称: Oracle.ManagedDataAccess.Client
项目位置:
下载位置:    https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core/

第二步:配置数据源,修改Config目录中的配置文件DataSource.config,示例如下:

<?xml version="1.0" encoding="utf-8"?>
<DataSources>
  <DataSource>
    <Name>001</Name>
    <Type>SQLite</Type>
    <Provider>System.Data.SQLite</Provider>
    <ConnectionString>Data Source=c:\mydb.db;Version=3;Password=myPassword;</ConnectionString>
  </DataSource>
  <DataSource>
    <Name>002</Name>
    <Type>SQLServer</Type>
    <Provider>System.Data.SqlClient</Provider>
    <ConnectionString>Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;</ConnectionString>
  </DataSource>
  <DataSource>
    <Name>003</Name>
    <Type>Oracle</Type>
    <Provider>Oracle.ManagedDataAccess.Client</Provider>
    <ConnectionString>Data Source=127.0.0.1:1521/orcl;User ID=scott;Password=tiger;Persist Security Info=True;</ConnectionString>
  </DataSource>
  <DataSource>
    <Name>004</Name>
    <Type>MySql</Type>
    <Provider>MySql.Data.MySqlClient</Provider>
    <ConnectionString>Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;</ConnectionString>
  </DataSource>
</DataSources>

说明:
1.更多数据库连接字符串(ConnectionString)请参考:https://www.connectionstrings.com/
2.对于文件数据库,支持占位符{AppPath},例如:Provider=Microsoft.Jet.OLEDB.4.0;Data Source={AppPath}mydatabase.mdb;User Id=admin;Password=;

第三步:使用:

    using (ISession session = PersistentManager.Instance.OpenSession("007"))
    {
        // 返回DataTable
        var dt = session.ExecuteQuery("select * from movie");
        Console.WriteLine(dt?.Rows.Count);
        
        // 返回List<T>
        var list = session.ExecuteQuery<Movie>("select * from movie");
        Console.WriteLine(list.Count);
    }

性能测试(Oracle)(Test\ConsoleApp\bin\Debug\netcoreapp3.1\ConsoleApp.exe)

从数据库查询记录4100条, 返回DataTable, 消耗1238毫秒。本地Linq查询记录2000条, 消耗19毫秒。
从数据库查询记录4100条, 返回List<T>, 消耗24毫秒。本地Linq查询记录2000条, 消耗0毫秒。

(具体性能视配置而定)(你需要明白返回List<T>比DataTable快的原因,是因为采用仅向前游标查询数据,而不是写法有多牛逼,速度的快慢取决于数据库底层驱动。并且DataTable和List<T>的数据结构和应用场景不同,各取所需,无可比性。)

备注:DbCore依赖log4net,已经放在包中了,引用即可,不需要log4net.config配置文件。
最后,DbCore并没有什么花哨的东西,它只是合理管理和简单实用。

QQ讨论群:948127686

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.  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. 
.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.
  • .NETStandard 2.0

    • No dependencies.

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.2.0 1,238 3/11/2020