SqlSugar 是 .net中唯一支持原生连接TDengine的ORM,非HTTP连接使用原生连高性能,并且支持微秒和纳秒
SqlSugar.TDengineCore 4.1.4+
SqlSugarCore 5.1.4.129 +
老版本升级:注意配一下 typeof(CommonPropertyConvert)看下面代码
//程序启动时加入(这个只要执行一次) InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] { typeof(TDengineProvider).Assembly }; //创建 db对象 var db = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.TDengine, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true, ConfigureExternalServices = new ConfigureExternalServices() { EntityService= (property, column) => { if (column.SqlParameterDbType == null) { //需要给列加上通用转换,这样实体就不需要一个一个转了 column.SqlParameterDbType = typeof(CommonPropertyConvert); } } } });
变更说明:给所有列加上通用转换,在EntityService中加
Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power //默认是毫秒,要微秒等看下面有介绍
//程序启动时加入 InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] { typeof(TDengineProvider).Assembly }; var db = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.TDengine, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true, AopEvents = new AopEvents { OnLogExecuting = (sql, p) => { Console.WriteLine(UtilMethods.GetNativeSql(sql, p)); } }, ConfigureExternalServices = new ConfigureExternalServices() { EntityService= (property, column) => { if (column.SqlParameterDbType == null) { //所有列加上转换 column.SqlParameterDbType = typeof(CommonPropertyConvert); } } } });
//程序启动时加入 InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] { typeof(TDengineProvider).Assembly }; var db= new SqlSugarClient(new ConnectionConfig(){ DbType = SqlSugar.DbType.PostgreSQL, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true, MoreSettings= new ConnMoreSettings() { PgSqlIsAutoToLower = false,//增删查改支持驼峰表 PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30 }} , ConfigureExternalServices = new ConfigureExternalServices() { EntityService= (property, column) => { if (column.SqlParameterDbType == null) { //所有列加上转换 column.SqlParameterDbType = typeof(CommonPropertyConvert); } } } )
当前程序需要安装Sdk才能连接数据库
https://docs.taosdata.com/connector
下面是NUGET安装
SqlSugar.TDengineCore SqlSugarCore
//程序启动时加入 InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] { typeof(TDengineProvider).Assembly }; //db对象 SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){ ConnectionString = "连接符字串", DbType = DbType.TDengine, IsAutoCloseConnection = true});
新支持请升级:SqlSugar.TDengineCore 2.1+
//手动赋值 [SugarColumn(IsPrimaryKey = true)] public DateTime ts { get; set; } //数据库默认值(推荐) [SugarColumn(IsPrimaryKey =true,InsertServerTime =true)] //ORM 默认字符串建毫秒库 db.DbMaintenance.CreateDatabase();
注意:连接字符串加上 TsType=config_us 不然条件过滤不了
//连接字符串加上 TsType=config_us [SugarColumn(IsPrimaryKey = true,SqlParameterDbType =typeof(DateTime16))] public DateTime ts { get; set; } //ORM 建库方法创建库 字符串加上 TsType=config_us 可以创建微秒库 db.DbMaintenance.CreateDatabase();
注意:连接字符串加上 TsType=config_ns 不然条件过滤不了
//TsType=config_ns [SugarColumn(IsPrimaryKey = true,SqlParameterDbType =typeof(DateTime19))] public DateTime ts { get; set; } //ORM 建库方法创建库 字符串加上 TsType=config_ns 可以创建纳秒库 db.DbMaintenance.CreateDatabase();
//建库 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<MyTable02>().Where(it => it.name == "测试2").ToList(); var list22 = db.Queryable<MyTable02>().Where(it => it.voltage == 222).ToList(); var list222 = db.Queryable<MyTable02>().Where(it => it.phase == 1.2).ToList(); var list2222 = db.Queryable<MyTable02>().Where(it => it.isdelete == true).ToList();
//联表查询:不支持left join只能这样 var list101= db.Queryable<MyTable02, MyTable02>((x, y) => x.ts == y.ts) .Select((x,y) => new { xts=x.ts, yts=y.ts }).ToList();
//模糊查询 var list3 = db.Queryable<MyTable02>().Where(it => it.name.Contains("a")).ToList(); //时间差函数 var list31 = db.Queryable<MyTable02>().Select(it => new { diff = SqlFunc.DateDiff(DateType.Day, it.ts, DateTime.Now), time=it.ts }).ToList(); //时间加1天 var list32 = db.Queryable<MyTable02>().Select(it => new { addTime = SqlFunc.DateAdd(it.ts,1, DateType.Day), oldime = it.ts }).ToList(); //自定义函数:实现时间加1天 var list33 = db.Queryable<MyTable02>().Select(it => new { addTime =SqlFunc.MappingColumn<DateTime>(" `ts`+1d "), oldime = it.ts }).ToList();
//分页 var Count = 0; var list4 = db.Queryable<MyTable02>().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<MyTable02>().Where(it => it.ts>time).ExecuteCommand();
创建表以子表为准(超级表名字是固定的前缀+表名) CRUD也是子表为准
db.CodeFirst.InitTables<CodeFirst03>();//建表 db.Insertable(new CodeFirst03() { Ts = DateTime.Now, Boolean = true, Char = 'a', Decimal = Convert.ToDecimal(18.2), Int16 = 16, Int32 = 32, Int64 = 64, String = "string", SByte=3, Byte = 2, Decimal2 = Convert.ToDecimal(18.3), Double = Convert.ToDouble(18.44), Float = Convert.ToSingle(18.45), String2 = "2", UInt16=116, UInt32=332, UInt64=664 }).ExecuteCommand(); var list3 = db.Queryable<CodeFirst03>().ToList(); public class CodeFirst03 : STable //需要继承 STable { [SqlSugar.SugarColumn(IsPrimaryKey = true)] public DateTime Ts { get; set; } public bool Boolean { get; set; } public byte Byte { get; set; } public sbyte SByte { get; set; } public char Char { get; set; } public decimal Decimal { get; set; } [SqlSugar.SugarColumn(Length = 18, DecimalDigits = 2)] public decimal Decimal2 { get; set; } [SqlSugar.SugarColumn(Length = 18, DecimalDigits = 2)] public double Double { get; set; } public float Float { get; set; } public int Int32 { get; set; } public uint UInt32 { get; set; } public long Int64 { get; set; } public ulong UInt64 { get; set; } public short Int16 { get; set; } public ushort UInt16 { get; set; } public string String { get; set; } [SqlSugar.SugarColumn(Length = 100)] public string String2 { get; set; } }
注意需要继承:STable
STableName 是超级表的名字
//子表1 ( 子表名和Tags不一样,其他都要一样) [STableAttribute( STableName = "CodeFirstStable", Tags="[{ Name:\"Tag1\",Value:\"1\"}]")] public class CodeFirstTags44 { [SqlSugar.SugarColumn(IsPrimaryKey = true)] public DateTime Ts { get; set; } public bool Boolean { get; set; } [SqlSugar.SugarColumn(IsIgnore =true)] public string Tag1 { get; set; } } //子表2 ( 子表名和Tags不一样,其他都要一样) [STableAttribute(STableName = "CodeFirstStable", Tags = "[{ Name:\"Tag1\",Value:\"2\"}]")] public class CodeFirstTags33 { [SqlSugar.SugarColumn(IsPrimaryKey = true)] public DateTime Ts { get; set; } public bool Boolean { get; set; } [SqlSugar.SugarColumn(IsIgnore = true)] public string Tag1 { get; set; } } //建表 db.CodeFirst.InitTables<CodeFirstTags33>(); db.CodeFirst.InitTables<CodeFirstTags44>(); //操作子表 db.Insertable(new CodeFirstTags33() { Boolean = true, Ts = DateTime.Now }).ExecuteCommand(); db.Insertable(new CodeFirstTags44() { Boolean = true, Ts = DateTime.Now }).ExecuteCommand();
找到TDengineTests文件夹
https://github.com/DotNetNext/SqlSugar
升级:5.1.4.114-preview+
//程序启动时加入 InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] { typeof(TDengineProvider).Assembly };
2016 © donet5.comApache Licence 2.0