.NET 操作 TDengine .NET ORM

TDengine 

是国内比较流的时序库之一,支持群集并且免费,在.NET中资料比较少,这篇文章主要介绍SqlSugar ORM来操作TDengine

优点:

1、SqlSugar支持ADO.NET操作来实现TDengine,并且支持了常用的时间函数、支持联表、分页和批量插入和删等操作

2、使用了原生连接(非http连接 )保证了TDengine性能

 

TDengine 连接字符串

Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power

TDengine 客户端SDK安装

当前程序需要安装Sdk才能连接数据库

https://docs.taosdata.com/connector

TDengine Nuegt安装

下面是NUGET安装

SqlSugar.TDengineCore
SqlSugarCore

执行SQL

    //建库
    db.Ado.ExecuteCommand("CREATE DATABASE IF NOT EXISTS power WAL_RETENTION_PERIOD 3600");
  
    //建超级表
    db.Ado.ExecuteCommand("CREATE STABLE IF NOT EXISTS  MyTable (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)");
  
    //创建子表
    db.Ado.ExecuteCommand(@"create table IF NOT EXISTS  MyTable01 using MyTable tags('California.SanFrancisco',1)");
  
          
    //insert sql
    //db.Ado.ExecuteCommand(insrtSql);
  
    //查询子表
    var dt = db.Ado.GetDataTable("select * from MyTable01");
  
    //查询超级表
    var dt2 = db.Ado.GetDataTable("select * from MyTable");

单表查询

var list2 = db.Queryable().Where(it => it.name == "测试2").ToList();
 var list22 = db.Queryable().Where(it => it.voltage == 222).ToList();
 var list222 = db.Queryable().Where(it => it.phase == 1.2).ToList();
 var list2222 = db.Queryable().Where(it => it.isdelete == true).ToList();

联表查询

  //联表查询:不支持left join只能这样
   var list101= db.Queryable((x, y) => x.ts == y.ts)
                .Select((x,y) => new
                {
                    xts=x.ts,
                    yts=y.ts
                }).ToList();

函数用法

  //模糊查询
  var list3 = db.Queryable().Where(it => it.name.Contains("a")).ToList();
 
 //时间差函数 
 var list31 = db.Queryable().Select(it =>
            new
            {
                diff = SqlFunc.DateDiff(DateType.Day, it.ts, DateTime.Now),
                time=it.ts
            }).ToList();
 
  //时间加1天
  var list32 = db.Queryable().Select(it =>
              new
              {
                  addTime = SqlFunc.DateAdd(it.ts,1, DateType.Day),
                  oldime = it.ts
              }).ToList();
               
  //自定义函数:实现时间加1天
    var list33 = db.Queryable().Select(it =>
              new
              {
                  addTime =SqlFunc.MappingColumn(" `ts`+1d "),
                  oldime = it.ts
              }).ToList();

分页查询

   //分页
   var Count = 0;
  var list4 = db.Queryable().Where(it => it.voltage == 111)
                .ToPageList(1, 2, ref Count);

插入

超级表是不能插入的,我们只插入子表

//批量插入子表
  db.Insertable(GetInsertDatas()).ExecuteCommand();
 
  //子表实体
  public class MyTable02
  {
            [SugarColumn(IsPrimaryKey =true)]
            public DateTime ts { get; set; }
            public float current { get; set; }
            public bool isdelete { get; set; }
            public string name { get; set; }
            public int voltage { get; set; }
            public float phase { get; set; }
            [SugarColumn(IsOnlyIgnoreInsert =true,IsOnlyIgnoreUpdate =true)]//Tags字段禁止插入
            public string location { get; set; }
            [SugarColumn(IsOnlyIgnoreInsert = true, IsOnlyIgnoreUpdate = true)]//Tags字段禁止插入
            public int groupId { get; set; }
   }

删除数据

只能范围删除不能直接等于删除

var count = db.Deleteable().Where(it => it.ts>time).ExecuteCommand();

源码和DEMO

找到TDengineTests文件夹

https://github.com/DotNetNext/SqlSugar

.NET 操作 TDengine .NET ORM_第1张图片

 

你可能感兴趣的:(.net,tdengine,数据库)