DbCRUD.Mysql 1.6.0

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

// Install DbCRUD.Mysql as a Cake Tool
#tool nuget:?package=DbCRUD.Mysql&version=1.6.0                

安装

Install-Package DbCRUD.Mysql 安装包

数据库连接及初始化

 //数据库连接 Database connection
 IDbCRUD testdb = new MysqlCRUD(@"Server=127.0.0.1;Database=testdb;Uid=root;Pwd=;");

声明

 const string autoIDData = "autoIDData";
  string tb_custormer = "custormer";
  const string dictable = "dicdata";
  const string sqldata = "Sqldata";
  const string tb_jsondata = "jsondata";
  Random random = new Random();

插入数据

//获取最大ID,用于ID编号
           int dbcount = testdb.TableExists(tb_custormer) ? testdb?.Count(tb_custormer) ?? 0 : 0;
           //**同步插入对象数据 Insert object data synchronously
           var customer = new CrudTestModel
           {
               ID = dbcount + 1, //如果要使用DB自身编号功能,ID不要赋值即可
               Name = "objectData",
               Phones = new string[] { "80000", "90000" },
               IsActive = true,
           };
           var result = testdb.Insert(tb_custormer, customer);

alternate text is missing from this package README image

//**同步插入字典数据 Insert dictionary data synchronously
   var dic1 = new Dictionary<string, object>
   {
       //{ "ID", 1 },//***如果不指定ID,插入时会自动编一个int的唯一ID
       { "Name", "dictionary data" },
       { "Qty",random.Next(1,10000) },
       { "DDate", DateTime.Now }
   };
   var result11 = testdb.Insert(autoIDData, dic1);
//**批量插入列表 Batch insert
   List<Dictionary<string, object>> listdata = new List<Dictionary<string, object>>();
   int maxid = testdb.Max<int>(dictable);
   for (int i = 0; i < 10; i++)
   {
       maxid++;
       var dic2 = new Dictionary<string, object>
       {
           { "ID",maxid },
           { "Name", "Batch insert" },
           { "Qty",random.Next(1,10000) },
           { "DDate", DateTime.Now }
       };
       listdata.Add(dic2);
   }
   var listResult = testdb.Insert(dictable, listdata);

更新数据

  var updata = new Dictionary<string, object>
    {
        { "Name", "更新指定字段数据" },
        { "Qty", 600}
    };
    var upresult = testdb.UpDate(dictable, updata, "ID=6");   //更新ID=2的数据
    Assert.IsTrue(upresult.Stutas);
    //更新后
    var getupdata = testdb.Find<Dictionary<string, object>>(dictable, "ID=6")?.FirstOrDefault();

alternate text is missing from this package README image

更新及插入数据(数据存在更新,不存在插入)

//** 更新或插入数据 Update or insert data
   var dic1 = new Dictionary<string, object>
   {
       { "ID", 2 },
       { "Name", "Insert or update" },
       { "Qty", 200},
       { "DDate", DateTime.Now }
   };
   var result= testdb.Upsert(dictable, dic1);
//** Batch insert or update
   var dic3 = new Dictionary<string, object>
   {
       { "ID", 3 },
       { "Name", "Batch insert or update" },
       { "Qty", 300},
       { "DDATE", DateTime.Now }
   };
   List<Dictionary<string,object>> listdata=new List<Dictionary<string, object>> { dic3,dic1};
   var listresult = testdb.Upsert(dictable, listdata);

查询数据

   //查找id=2的数据
    var databyid = testdb.FindByID<Dictionary<string, object>>(dictable, 2);
   
    //查找Qty>10的数据
    var wheredata = testdb.Find<Dictionary<string, object>>(dictable, "Qty>10");

    //sql语句查找的数据
    string sqlcmd = $"select * from {dictable}";
    var sqldata = testdb.Find<Dictionary<string, object>>(sqlcmd);
   
    //【返回实体数据】查找id=2的数据
    var crud_result = testdb.FindAndResult<CrudTestModel>(tb_custormer, "id=2");
    

    ///【查询单条数据】查找id>2的数据,返回按DDATE倒序,并排除dic列的最新一条数据
    var one_crud_result = testdb.FindOne<CrudTestModel>(tb_custormer, "id=2", project: "!dic", sort: "!DDATE");
    
    //【SQL语法模糊查询】,查找name中'Litedb'开头的数据,不区分大小写
    var like_result = testdb.FindAndResult<Dictionary<string, object>>(autoIDData, "Name like'dictionary%'");
   

    //【简写语法】分页查找Qty>10,返回除ID列,按DDATE倒序排序的数据,返回第一页10条数据。
    var page_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(tb_jsondata, "Qty>10", project: "!ID", sort: "!DDATE", pageindex: 1, pagecount: 10);
    
    //【SQL语法】分页查找Qty>10,返回除ID列,按DDATE倒序排序的数据,返回第一页10条数据。
    var sqlite_pagedata = testdb.GetPagingDataAndResult<Dictionary<string, object>>(tb_jsondata, "Qty>10", project: "name,ddate", sort: "DDATE desc", pageindex: 1, pagecount: 10);
    
    //【返回DataTable】分页查找Qty>10,返回除ID列,按DDATE倒序排序的数据,返回第一页10条数据。
    var datatable_pagedata = testdb.GetPagingData(autoIDData, "Qty>10", project: "!ID", sort: "!DDATE", pageindex: 1, pagecount: 10);
    
    //分页查找的数据,返回全部指定列到DataTable
    var dt = testdb.GetPagingData(dictable, "Qty>6", project: "id,qty,ddate", pageindex: 1, pagecount: 10);
    ;
    //【多条件查询】分页查找Qty>10,返回除ID列,按DDATE倒序排序的数据,返回第一页10条数据。
    var mu_where = testdb.GetPagingData<Dictionary<string, object>>(dictable, "ID>=6 and Qty>10", sort: "!DDATE", pageindex: 1, pagecount: 10);
    
    //【日期范围查询】查找DDATE>='2023-06-05 09:12:24' 和 DDATE<='2023-06-05 13:28:48'的数据。
    var date_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, $"DDATE>='2023-06-05 09:12:24' and DDATE<='{DateTime.Now:yyyy-MM-dd HH:mm:ss}'");
    
    //【SQL语法in查询】查找Qty=200和300的数据。
    var sql_in_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, "Qty in(200,300)");
    
    //【in模糊查询】查找Name=Batch insert和data结尾的数据。不支持模糊匹配。
    var in_fuzzy = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, "Name in('Batch insert','data')");
    //【委托查询】查找_id>=6 and Qty>10的数据。数据在委托中返回,方便进行数据处理
    var action_result = testdb.GetPagingDataAction<CrudTestModel1>(dictable, "_id>=6 and Qty>10", datalist => 
    {
        double sum = datalist.Sum(s => s.FFloat);
                
    });
    

alternate text is missing from this package README image

删除数据

   //**删除_id=3的数据
   var result = testdb.Delete(dictable,3);
   //**删除qty<30的数据
   var wherresult = testdb.Delete(dictable, "Qty<30");
   //**使用sql语句删除_id=30的数据
   string sql = $"delete {dictable} where _id=30";
   var sqlresult = testdb.Delete(sql);

Key-Value操作,常用于配置信息

string key = "test"; var v = DateTime.Now; //保存 var result = testdb.SaveKeyValue(key, v); //判断是否存在 bool isExists= testdb.KeyValueExists(key) //获取 var getv = testdb.GetKeyValue<DateTime>(key); //删除 bool ok= testdb.DelKeyValue(key)

消息事件绑定(可日志输出)

    public DbTest() {
    t estdb.Message += Testdb_Message;
    }

    private void Testdb_Message((string Message, string Level, DateTime Time) obj)
    {
       Debug.WriteLine($"{obj.Time}|{obj.Level}|{obj.Message}");
    }

增删改查系列包

一致的增删改查语法

LiteDB

IDbCRUD testdb = new LiteDbCRUD(@"filename=CRUDTestDB.db");
//【日期范围查询】查找DDATE>='2023-06-05 09:12:24' 和 DDATE<='2023-06-05 13:28:48'的数据。
var date_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, $"DDATE>='2023-06-05 09:12:24' and DDATE<='{DateTime.Now:yyyy-MM-dd HH:mm:ss}'");

MongoDB

IDbCRUD testdb =new MongoDbCRUD("mongodb://localhost:27017/testdb");
//【日期范围查询】查找DDATE>='2023-06-05 09:12:24' 和 DDATE<='2023-06-05 13:28:48'的数据。
var date_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, $"DDATE>='2023-06-05 09:12:24' and DDATE<='{DateTime.Now:yyyy-MM-dd HH:mm:ss}'");

Mysql

IDbCRUD testdb = new MysqlCRUD(@"Server=127.0.0.1;Database=testdb;Uid=root;Pwd=;");

//【日期范围查询】查找DDATE>='2023-06-05 09:12:24' 和 DDATE<='2023-06-05 13:28:48'的数据。
var date_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, $"DDATE>='2023-06-05 09:12:24' and DDATE<='{DateTime.Now:yyyy-MM-dd HH:mm:ss}'");

Sqlite

IDbCRUD testdb = new SqliteCRUD($@"Data Source=sqlitedb.db; Cache=Shared")

//【日期范围查询】查找DDATE>='2023-06-05 09:12:24' 和 DDATE<='2023-06-05 13:28:48'的数据。
var date_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, $"DDATE>='2023-06-05 09:12:24' and DDATE<='{DateTime.Now:yyyy-MM-dd HH:mm:ss}'");

SQL SERVER

IDbCRUD testdb = new SqlServerCRUD(@"Data Source=xxx;Initial Catalog=xxx;User ID=sa;Password=xxx;Encrypt=True;TrustServerCertificate=True;");
//【日期范围查询】查找DDATE>='2023-06-05 09:12:24' 和 DDATE<='2023-06-05 13:28:48'的数据。
var date_result = testdb.GetPagingDataAndResult<Dictionary<string, object>>(dictable, $"DDATE>='2023-06-05 09:12:24' and DDATE<='{DateTime.Now:yyyy-MM-dd HH:mm:ss}'");

实体模型

 public class CrudTestModel
    {
        [BsonId]
        public int ID { get; set; }
        public string Name { get; set; }
        public string[] Phones { get; set; }
        public bool IsActive { get; set; }
        public Dictionary<string, object> Dic { get; set; }
        public double FFloat { get; set; } = 0.118;
        public DateTime DDATE { get; set; } = DateTime.Now;

    }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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 is compatible.  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 is compatible. 
.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
1.7.1 97 6/24/2024
1.7.0 116 4/8/2024
1.6.1 129 4/6/2024
1.6.0 122 3/22/2024
1.5.12 125 3/21/2024
1.5.11 167 8/27/2023
1.5.10 152 8/4/2023
1.5.8 145 7/27/2023
1.5.7 148 7/26/2023
1.5.6 148 7/20/2023
1.5.5 138 6/26/2023
1.5.4 141 6/18/2023
1.5.3 130 6/13/2023
1.5.2 143 6/11/2023
1.5.1 130 6/9/2023
1.5.0 135 6/8/2023
1.4.0 134 6/6/2023
1.3.1 142 5/17/2023
1.3.0 142 5/14/2023
1.2.0 151 5/11/2023
1.1.0 150 5/10/2023
1.0.0 149 5/9/2023

1. 增加key-value
2. 增加linq查询