JCoder.Db4Net 2.20.12

There is a newer version of this package available.
See the version list below for details.
dotnet add package JCoder.Db4Net --version 2.20.12                
NuGet\Install-Package JCoder.Db4Net -Version 2.20.12                
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="JCoder.Db4Net" Version="2.20.12" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JCoder.Db4Net --version 2.20.12                
#r "nuget: JCoder.Db4Net, 2.20.12"                
#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 JCoder.Db4Net as a Cake Addin
#addin nuget:?package=JCoder.Db4Net&version=2.20.12

// Install JCoder.Db4Net as a Cake Tool
#tool nuget:?package=JCoder.Db4Net&version=2.20.12                

JCoder.Db4Net

属性
作者(Authors) Jackie Law
版权(Copyright) Copyright © Jackie Law. All rights reserved.
协议 MIT
联系(QQ) 1378071853
联系(Wechat) JackieLaw1024
联系(Email) JackieLaw-dev@outlook.com

[TOC]


架构说明

JCoder.Db4Net
语言 C#
协议 MIT
.Net框架 .Net Standard2.1; .Net Framework 4.7.2; .Net 5; .Net 6;
简述 Database类库,一款为方便使用数据库的操作类库。

引用类库

引用类库 版本
- -

相关类库

相关类库 说明
JCoder.Db4Net 一款为方便使用数据库的操作类库。
JCoder.Db4Net.ORM 一个基于JCoder.Db4Net的ORM库。
JCoder.Db4Net.Sqlite 一个基于JCoder.Db4Net且支持Sqlite的库。
JCoder.Db4Net.MySql 一个基于JCoder.Db4Net且支持Mysql的库。
JCoder.Db4Net.SqlServer 一个基于JCoder.Db4Net且支持SqlServer的库 。
JCoder.Mock4Net 一款简易版的用于模拟数据的功能库。
JCore.Log4Net 一款用于日志的类库。

获取方式

  1. 版本要求:>=2.20.6
  2. 以版本2.20.6为例
获取方式 描述
Package Manager Install-Package JCoder.Db4Net -Version 2.20.6
.Net CLI dotnet add package JCoder.Db4Net --version 2.20.6
PackageReference <PackageReference Include="JCoder.Db4Net" Version="2.20.6" />
Paket CLI paket add JCoder.Db4Net --version 2.20.6
Script&Interactive #r "nuget: JCoder.Db4Net, 2.20.6"
Cake // Install as a Cake Addin<br/>#addin nuget:?package=JCoder.Db4Net&version=2.20.6<br/><br/>// Install as a Cake Tool<br/>#tool nuget:?package=JCoder.Db4Net&version=2.20.6

使用说明

主要类与接口

名称 类型 描述
IDbSqlTexter 接口 主要用于SQL语句的生成。
IDbBuilder 接口 主要用于数据库结构的处理。
IDbOperator 接口 主要用于SQL的执行与处理。
IFactoryDbOperator 接口 通过ProviderFactory方式,进行SQL的执行与处理。<br/>主要有以下实现:MsSqlFactoryOperator、SQLiteFactoryOperator 、MySqlFactoryOperator 。
Db 工具类,主要用于数据库通用的处理。

创建实例-IDbOperator\IFactoryDbOperator

  1. 创建实例
    
    // MySql, 需要先在项目里引用或加载MySql官方库MySql.Data.dll。
    IDbOperator _dber = new MySqlFactoryOperator()
    {
        ConnectionString = "server=127.0.0.1;database=test;uid=user;pwd=testpassword;Connect Timeout=18000"
    };
    
    // SQLite, 需要先在项目里引用或加载Sqlite官方库System.Data.SQLite.dll。
    IDbOperator _dber = new SQLiteFactoryOperator()
    {
        ConnectionString = "Data Source=.\data\test.db3;"
    };
    
    // Sql Server, 需要先在项目里引用或加载Sql Server官方库System.Data.SqlClient.dll。
    IDbOperator _dber = new MsSqlFactoryOperator()
    {
        ConnectionString = "Server=127.0.0.1;Database=test;Uid = user; Pwd=testpassword;"
    };
    
    
  2. 使用创建全局实例
    
    // MySql
    IDbOperator _dber = Db.Hub.GetDberOrNew<MySqlFactoryOperator>();
    _dber.ConnectionString = "server=127.0.0.1;database=test;uid=user;pwd=testpassword;Connect Timeout=18000";
    
    

IDbOperator使用

  1. CanConnect(是否能够连接数据库)
    // CanConnect(是否能够连接数据库)
    if (_dber.CanConnect()) { return; }
    
  2. CreateParameter(创建相应的参数)
    // CreateParameter(创建相应的参数)
    return _dber.CreateParameter( "ID", 123);
    
  3. Execute(执行SQL)
    // Execute(执行SQL)
    var _parameters = new List<DbParameter>()
    {
        _dber.CreateParameter("ID","ID123"),
        _dber.CreateParameter("Name","Name123"),
        _dber.CreateParameter("CreateTime",DateTime.Now),
    };
    var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
    var _count = _dber.Execute(_sql, _parameters);//直接执行SQL
    //var _count = _dber.Execute(true,_sql, _parameters);// 使用事务执行SQL
    return _count >= 1;
    
  4. Execute(执行多条SQL)
    // Execute(执行多条SQL)
    var _parameters = new List<DbParameter>()
    {
        _dber.CreateParameter("Value1","ID123"),
        _dber.CreateParameter("Value2","Name123"),
        _dber.CreateParameter("Value3","Name1456"),
    };
    var _sqls = new string[]
    {
        @"insert table3 (Values) values (@Value1);",
        @"insert table3 (Values) values (@Value2);",
        @"insert table3 (Values) values (@Value3);",
    } ;
    var _count = _dber.Execute(_sqls, _parameters);//直接执行SQL
    //var _count = _dber.Execute(true, _sqls, _parameters);// 使用事务执行SQL
    return _count >= 1;
    
  5. ExecuteTransaction(使用事务执行指定的SQL)
    // ExecuteTransaction(使用事务执行指定的SQL)
    public void ExecuteTransaction()
    {
        _dber.StartTransaction();
    
         try
         {
            for (int i = 0; i < 3; i++)
            {
                 var _parameters = new List<DbParameter>()
                 {
                     _dber.CreateParameter("ID",i.ToString()),
                     _dber.CreateParameter("Name","Name123"),
                     _dber.CreateParameter("CreateTime",DateTime.Now),
                 };
                 var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
                 var _isSucceed = _dber.ExecuteTransaction(_sql, _parameters);
            }
         }
         catch (Exception ex)
         {
             _dber.RollbackTransaction();
             return;
         }
    
         _dber.FinishTransaction();
    }
    
    
  6. ExecuteTransaction(使用指定的事务执行指定的SQL)
    // ExecuteTransaction(使用指定的事务执行指定的SQL)
    public void ExecuteTransaction()
    {
        for (int i = 0; i < 3; i++)
        {
            var _key = "Transaction" + i.ToString();
            _dber.StartTransaction(_key);
    
            var _id = i.ToString() + DateTime.Now.ToLongTimeString();
            try
            {
                var _parameters = new List<DbParameter>()
                {
                    _dber.CreateParameter("ID",_id),
                    _dber.CreateParameter("Name","Name123"),
                    _dber.CreateParameter("CreateTime",DateTime.Now),
                };
                var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
                var _isSucceed = _dber.ExecuteTransaction(_key, _sql, _parameters);
            }
            catch (Exception ex)
            {
                _dber.RollbackTransaction(_key);
                return;
            }
    
            if (!_dber.FinishTransaction(_key))
            {
                return;
            }
        }
    }
    
    
  7. RollbackTransaction(回滚指定的事务)
    // RollbackTransaction(回滚指定的事务)
    public void RollbackTransaction()
    {
        var _key = "Transaction";
        _dber.StartTransaction(_key);
    
        var _id = nameof(RollbackTransaction);
    
        try
        {
            var _parameters = new List<DbParameter>()
            {
                _dber.CreateParameter("ID",_id),
                _dber.CreateParameter("Name","Name123"),
                _dber.CreateParameter("CreateTime",DateTime.Now),
            };
            var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
            var _isSucceed = _dber.ExecuteTransaction(_key, _sql, _parameters);
        }
        catch (Exception ex)
        {
            _dber.RollbackTransaction(_key);
            return;
        }
    
        _dber.RollbackTransaction(_key);
    }
    
  8. QueryTable(查询数据,并返回DataTable)
    public DataTable QueryTable()
    {
        var _parameters = new List<DbParameter>()
        {
            _dber.CreateParameter("ID",123),
        };
        var _sql = "select * from table1 where ID=@ID;";
        return _dber.QueryTable(_sql, _parameters);
    }
    
  9. QueryTables(查询数据,并返回DataSet)
    public DataSet QueryTables()
    {
        var _parameters = new List<DbParameter>()
        {
            _dber.CreateParameter("ID",123),
        };
        var _sql = "select * from table1 where ID=@ID;";
        _sql += "select * from table2 where ID=@ID;";
        return _dber.QueryTables(_sql, _parameters);
    }
    
  10. QueryReader(查询数据,并返回DbDataReader)
public QueryReader QueryReader()
{
    var _parameters = new List<DbParameter>()
    {
        _dber.CreateParameter("ID",123),
    };
    var _sql = "select * from table1 where ID=@ID;";
    return _dber.QueryReader(_sql, _parameters);
}
  1. QueryDictionary (将查询结果的首两列数据转换为Dictionary)
public Dictionary<string, string> QueryDictionary()
{
    var _sql = "select Key,Value from table1 ;";
    return _dber.QueryDictionary<string, string>(_sql);
}
  1. QueryValues (将查询结果的首列数据转换为List)
public List<string> QueryNames()
{
    var _sql = "select Names from table1 ;";
    return _dber.QueryValues<string>(_sql);
}
  1. QueryValue (将查询值转换为long)
public List<long> QueryValue()
{
    var _sql = "select count(1) from table1 ;";
    return _dber.QueryScalar<long>(_sql);
}
  1. IsExistData (查询是否存在数据)
public bool IsExistData()
{
    var _sql = "select 1 from table1 where id=1;";
    return _dber.IsExistData(_sql);
}

IDbSqlTexter使用

  1. 生成实例
    
    // 用于生成MySql语句,引用JCoder.Db4Net.MySql
    IDbSqlTexter _sqltexter = new MySqlTexter();
    
    // 用于生成Sqlite语句,引用JCoder.Db4Net.Sqlite
    IDbSqlTexter _sqltexter = new SqliteTexter();
    
    // 用于生成SqlServer语句,引用JCoder.Db4Net.SqlServer
    IDbSqlTexter _sqltexter = new MsSqlTexter();
    
    
  2. 生成数据库相关的SQL语句
    
    // 生成获取当前数据库信息的SQL语句
    _sqltexter.GetDatabase();
    
     // 生成获取指定数据库信息的SQL语句
    _sqltexter.GetDatabase("database");
    
     // 生成获取数据库信息列表的SQL语句
    _sqltexter.GetDatabaseList();
    
     // 生成创建数据库的SQL语句
    _sqltexter.CreateDatabase("database");
    
     // 生成删除数据库的SQL语句
    _sqltexter.DeleteDatabase("database");
    
    
  3. 生成数据表(Table)相关的SQL语句
    
    // 生成获取当前数据库的指定数据表的数据列信息的SQL语句
    _sqltexter.GetTableColumns("table01");
    // 生成获取指定数据库的指定数据表的数据列信息的SQL语句
    _sqltexter.GetTableColumns("database","table01");
    
     // 生成获取当前数据库的指定数据表信息的SQL语句SQL语句
    _sqltexter.GetTable("table01");
     // 生成获取当前数据库的指定数据表信息的SQL语句SQL语句
    _sqltexter.GetTable("database","table01");
    
     // 生成获取当前数据库的数据表列表的SQL语句
    _sqltexter.GetTableList();
     // 生成获取指定数据库的数据表列表的SQL语句
    _sqltexter.GetTableList("database");
    
     // 生成删除当前数据库的数据表的SQL语句
    _sqltexter.DeleteTable("table01");
     // 生成删除指定数据库的数据表的SQL语句
    _sqltexter.DeleteTable("database","table01");
    
     // 生成创建数据表的SQL语句
    _sqltexter.CreateTable("table01", new List<DbColumn>());
     // 生成创建数据表的SQL语句
    _sqltexter.CreateTable("database", "table01", new List<DbColumn>());
    
    
  4. 生成视图(View)相关的SQL语句
    
    // 生成获取当前数据库的指定View的数据列信息的SQL语句
    _sqltexter.GetViewColumns("view01");
    // 生成获取指定数据库的指定View的数据列信息的SQL语句
    _sqltexter.GetViewColumns("database","view01");
    
     // 生成获取当前数据库的指定View信息的SQL语句SQL语句
    _sqltexter.GetView("view01");
     // 生成获取当前数据库的指定View信息的SQL语句SQL语句
    _sqltexter.GetView("database","view01");
    
     // 生成获取当前数据库的View列表的SQL语句
    _sqltexter.GetViewList();
     // 生成获取指定数据库的View列表的SQL语句
    _sqltexter.GetViewList("database");
    
     // 生成删除当前数据库的View的SQL语句
    _sqltexter.DeleteView("view01");
     // 生成删除指定数据库的View的SQL语句
    _sqltexter.DeleteView("database","view01");
    
    
  5. 生成函数(Function)相关的SQL语句
    
    // 生成获取当前数据库的指定函数信息的SQL语句
    _sqltexter.GetFunction("func01");
    // 生成获取指定数据库的指定函数信息的SQL语句
    _sqltexter.GetFunction("database","func01");
    
     // 生成获取当前数据库的函数列表的SQL语句
    _sqltexter.GetFunctionList();
     // 生成获取指定数据库的函数列表的SQL语句
    _sqltexter.GetFunctionList("database");
    
     // 生成删除当前数据库的Function的SQL语句
    _sqltexter.DeleteFunction("func01");
     // 生成删除指定数据库指定函数的SQL语句
    _sqltexter.DeleteFunction("database","func01");
    
    
  6. 生成存储过程(Procedure)相关的SQL语句
    
    // 生成获取当前数据库的指定存储过程信息的SQL语句
    _sqltexter.GetProcedure("proc01");
    // 生成获取指定数据库的指定存储过程信息的SQL语句
    _sqltexter.GetProcedure("database","proc01");
    
     // 生成获取当前数据库的存储过程列表的SQL语句
    _sqltexter.GetProcedureList();
     // 生成获取指定数据库的存储过程列表的SQL语句
    _sqltexter.GetProcedureList("database");
    
     // 生成删除当前数据库的Procedure的SQL语句
    _sqltexter.DeleteProcedure("proc01");
     // 生成删除指定数据库指定存储过程的SQL语句
    _sqltexter.DeleteProcedure("database","proc01");
    
    
Product 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 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net472 is compatible.  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.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on JCoder.Db4Net:

Package Downloads
JCoder.Db4Net.Sqlite

A library based on JCoder.Db4Net and it support Sqlite. (一个基于JCoder.Db4Net且支持Sqlite的库)

JCoder.Db4Net.MySql

A library based on JCoder.Db4Net and it support Mysql.(一个基于JCoder.Db4Net且支持Mysql的库)

JCoder.Db4Net.SqlServer

A library based on JCoder.Db4Net and it support microsoft sql server. (一个基于JCoder.Db4Net且支持sql server的库)

JCoder.Extension.SqliteAssistKitsts

一款使用Sqlite进行辅助的工具库。

JCoder.Log4Net.Sqlite

A simple log message library by sqlite and it base on JCoder.Log4Net.(一款基于JCoder.Log4Net的用Sqlite记录信息的简易类库)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.14.5 150 11/14/2024
3.14.3 161 11/11/2024
3.11.1 138 11/1/2024
3.10.1 67 11/1/2024
3.9.8 253 9/17/2024
3.9.7 205 9/8/2024
3.9.6 203 8/30/2024
3.9.5 192 8/30/2024
3.9.4 191 8/30/2024
3.9.3 179 8/30/2024
3.9.2 204 8/30/2024
3.9.1 177 8/30/2024
3.8.3 239 8/13/2024
3.8.2 186 7/24/2024
3.8.1 178 7/18/2024
3.7.4 153 7/17/2024
3.7.3 255 4/5/2024
3.6.3 297 4/4/2024
3.5.3 207 1/3/2024
3.5.2 295 12/3/2023
3.5.1 324 10/5/2023
3.4.2 295 10/2/2023
2.26.2304.308 243 4/3/2023
2.21.4 1,629 11/8/2022
2.20.12 1,230 11/4/2022
2.19.5 1,502 10/14/2022
2.18.6 1,476 10/13/2022
2.13.3 1,477 6/5/2022
2.12.3 1,530 5/25/2022
2.11.4 1,553 5/13/2022

Please contact Jackie by QQ 1378071853 or E-mail JackieLaw-dev@outlook.com;