From 182d90eb353258959633ee1dbae936676bf20d41 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 2 Aug 2023 08:32:38 +0800 Subject: [PATCH] Update TDengine test --- .../SqlSugar.TDengineCore.csproj | 2 +- Src/Asp.NetCore2/TDengineTest/Demo/ADOTest.cs | 106 ++++++++++++++++++ .../{Demo0_SqlSugarClient.cs => ORMTest.cs} | 29 +++-- Src/Asp.NetCore2/TDengineTest/Program.cs | 8 +- 4 files changed, 127 insertions(+), 18 deletions(-) create mode 100644 Src/Asp.NetCore2/TDengineTest/Demo/ADOTest.cs rename Src/Asp.NetCore2/TDengineTest/Demo/{Demo0_SqlSugarClient.cs => ORMTest.cs} (78%) diff --git a/Src/Asp.NetCore2/SqlSugar.TDengineCore/SqlSugar.TDengineCore.csproj b/Src/Asp.NetCore2/SqlSugar.TDengineCore/SqlSugar.TDengineCore.csproj index c970b9f93..4b8a69788 100644 --- a/Src/Asp.NetCore2/SqlSugar.TDengineCore/SqlSugar.TDengineCore.csproj +++ b/Src/Asp.NetCore2/SqlSugar.TDengineCore/SqlSugar.TDengineCore.csproj @@ -6,7 +6,7 @@ - + diff --git a/Src/Asp.NetCore2/TDengineTest/Demo/ADOTest.cs b/Src/Asp.NetCore2/TDengineTest/Demo/ADOTest.cs new file mode 100644 index 000000000..13ec25952 --- /dev/null +++ b/Src/Asp.NetCore2/TDengineTest/Demo/ADOTest.cs @@ -0,0 +1,106 @@ +using SqlSugar.TDengineAdo; +using System; +using System.Collections.Generic; +using System.Data; +using System.Text; + +namespace TDengineTest +{ + internal class AdoDemo + { + public static void Init() + { + + ExecuteNonQuery();//库不存在建库 + ExecuteNonQuery2();//表不存在建表 + DataTable(); + DataReader(); + ExecuteScalar(); + ExecuteScalar2(); + Console.ReadKey(); + } + private static TDengineCommand ExecuteNonQuery() + { + TDengineConnection conn = + new TDengineConnection("Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power"); + conn.Open(); + + var comm = ((TDengineCommand)conn.CreateCommand()); + comm.Connection = conn; + comm.CommandText = "CREATE DATABASE IF NOT EXISTS power WAL_RETENTION_PERIOD 3600"; + var dr = comm.ExecuteNonQuery(); + + conn.Close(); + return comm; + } + private static TDengineCommand ExecuteNonQuery2() + { + TDengineConnection conn = + new TDengineConnection("Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power"); + conn.Open(); + + var comm = ((TDengineCommand)conn.CreateCommand()); + comm.Connection = conn; + string createTable = "CREATE STABLE IF NOT EXISTS test.meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);"; + comm.CommandText = createTable; + var dr = comm.ExecuteNonQuery(); + + conn.Close(); + return comm; + } + private static TDengineCommand DataTable() + { + TDengineConnection conn = + new TDengineConnection("Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power"); + conn.Open(); + + var comm = ((TDengineCommand)conn.CreateCommand()); + comm.Connection = conn; + comm.CommandText = "select * from power.meters "; + TDengineDataAdapter ds = new TDengineDataAdapter(comm); + var dt = new DataTable(); + ds.Fill(dt); + conn.Close(); + return comm; + } + private static TDengineCommand ExecuteScalar() + { + using TDengineConnection conn = + new TDengineConnection("Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power"); + conn.Open(); + + var comm = ((TDengineCommand)conn.CreateCommand()); + comm.Connection = conn; + comm.CommandText = "select count(*) from `power`.`meters` "; + var dr = comm.ExecuteScalar(); + return comm; + } + private static TDengineCommand ExecuteScalar2() + { + using TDengineConnection conn = + new TDengineConnection("Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power"); + conn.Open(); + + var comm = ((TDengineCommand)conn.CreateCommand()); + comm.Connection = conn; + comm.CommandText = "select count(*) from power.meters where ts=@ts"; + comm.Parameters.Add(new TDengineParameter("ts", Convert.ToDateTime("2018-10-03 14:38:05.000"))); + var dr = comm.ExecuteScalar(); + return comm; + } + private static TDengineCommand DataReader() + { + using TDengineConnection conn = + new TDengineConnection("Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power"); + conn.Open(); + var comm = ((TDengineCommand)conn.CreateCommand()); + comm.Connection = conn; + comm.CommandText = "select * from power.meters "; + var dr = comm.ExecuteReader(); + dr.Read(); + var xx = dr.GetInt32(2); + return comm; + } + + } +} diff --git a/Src/Asp.NetCore2/TDengineTest/Demo/Demo0_SqlSugarClient.cs b/Src/Asp.NetCore2/TDengineTest/Demo/ORMTest.cs similarity index 78% rename from Src/Asp.NetCore2/TDengineTest/Demo/Demo0_SqlSugarClient.cs rename to Src/Asp.NetCore2/TDengineTest/Demo/ORMTest.cs index e2d12e9fe..88b91550a 100644 --- a/Src/Asp.NetCore2/TDengineTest/Demo/Demo0_SqlSugarClient.cs +++ b/Src/Asp.NetCore2/TDengineTest/Demo/ORMTest.cs @@ -6,11 +6,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; -using TDengineDriver; +using TDengineDriver; namespace OrmTest { - public class Demo0_SqlSugarClient + public class ORMTest { public static void Init() @@ -34,27 +34,24 @@ namespace OrmTest 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 STABLE IF NOT EXISTS St01 (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT, isdelete BOOL, name BINARY(64)) 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); + db.Ado.ExecuteCommand(@"create table IF NOT EXISTS MyTable02 using St01 tags('California.SanFrancisco',1)"); + //查询子表 - var dt = db.Ado.GetDataTable("select * from MyTable01"); + var dt = db.Ado.GetDataTable("select * from MyTable02"); - //查询超级表 - var dt2 = db.Ado.GetDataTable("select * from MyTable"); //插入子表 - db.Insertable(new MyTable01() + db.Insertable(new MyTable02() { ts = DateTime.Now, current = Convert.ToSingle(1.1), groupId = 1, + isdelete=true, + name="haha", location = "aa", phase = Convert.ToSingle(1.1), voltage = 11 @@ -62,19 +59,21 @@ namespace OrmTest //查询子表(主表字段也能查出来) - var list = db.Queryable().ToList(); + var list = db.Queryable().ToList(); //删除子表 var ts = list.First().ts; - var count=db.Deleteable().Where(it=>it.ts==ts).ExecuteCommand(); + var count=db.Deleteable().Where(it=>it.ts==ts).ExecuteCommand(); } - public class MyTable01 + 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)] diff --git a/Src/Asp.NetCore2/TDengineTest/Program.cs b/Src/Asp.NetCore2/TDengineTest/Program.cs index 9233bb8b1..525714f07 100644 --- a/Src/Asp.NetCore2/TDengineTest/Program.cs +++ b/Src/Asp.NetCore2/TDengineTest/Program.cs @@ -1,5 +1,6 @@  using System; +using TDengineTest; namespace OrmTest { @@ -7,8 +8,11 @@ namespace OrmTest { static void Main(string[] args) { - //Demo - Demo0_SqlSugarClient.Init(); + //orm用例 + ORMTest.Init(); + //原生ado用例 + AdoDemo.Init(); + Console.WriteLine("all successfully."); Console.ReadKey();