SqlSugar/Src/Asp.NetCore2/TDengineTest/Demo/Demo0_SqlSugarClient.cs

89 lines
2.8 KiB
C#
Raw Normal View History

2023-07-31 18:55:12 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
2023-07-31 23:59:42 +08:00
using TDengineDriver;
2023-07-31 18:55:12 +08:00
namespace OrmTest
{
public class Demo0_SqlSugarClient
{
public static void Init()
{
2023-07-31 23:59:42 +08:00
var db = new SqlSugarClient(new ConnectionConfig()
2023-07-31 18:55:12 +08:00
{
2023-07-31 23:59:42 +08:00
DbType = SqlSugar.DbType.TDengine,
2023-07-31 18:55:12 +08:00
ConnectionString = Config.ConnectionString,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});
2023-07-31 23:59:42 +08:00
//建库
db.Ado.ExecuteCommand("CREATE DATABASE IF NOT EXISTS power WAL_RETENTION_PERIOD 3600");
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//建超级表
db.Ado.ExecuteCommand("CREATE STABLE IF NOT EXISTS MyTable (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)");
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//创建子表
db.Ado.ExecuteCommand(@"create table IF NOT EXISTS MyTable01 using MyTable tags('California.SanFrancisco',1)");
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//insert sql
//db.Ado.ExecuteCommand(insrtSql);
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//查询子表
var dt = db.Ado.GetDataTable("select * from MyTable01");
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//查询超级表
var dt2 = db.Ado.GetDataTable("select * from MyTable");
2023-07-31 18:55:12 +08:00
2023-08-01 00:28:46 +08:00
//插入子表
2023-07-31 23:59:42 +08:00
db.Insertable(new MyTable01()
2023-07-31 18:55:12 +08:00
{
2023-07-31 23:59:42 +08:00
ts = DateTime.Now,
current = Convert.ToSingle(1.1),
groupId = 1,
location = "aa",
phase = Convert.ToSingle(1.1),
voltage = 11
}).ExecuteCommand();
2023-08-01 00:28:46 +08:00
//查询子表(主表字段也能查出来)
2023-07-31 23:59:42 +08:00
var list = db.Queryable<MyTable01>().ToList();
2023-08-01 00:28:46 +08:00
//删除子表
var ts = list.First().ts;
var count=db.Deleteable<MyTable01>().Where(it=>it.ts==ts).ExecuteCommand();
2023-07-31 18:55:12 +08:00
}
2023-07-31 23:59:42 +08:00
public class MyTable01
2023-07-31 18:55:12 +08:00
{
2023-08-01 00:28:46 +08:00
[SugarColumn(IsPrimaryKey =true)]
2023-07-31 23:59:42 +08:00
public DateTime ts { get; set; }
public float current { get; set; }
public int voltage { get; set; }
public float phase { get; set; }
[SugarColumn(IsOnlyIgnoreInsert =true,IsOnlyIgnoreUpdate =true)]
public string location { get; set; }
[SugarColumn(IsOnlyIgnoreInsert = true, IsOnlyIgnoreUpdate = true)]
public int groupId { get; set; }
2023-08-01 00:28:46 +08:00
}
2023-07-31 18:55:12 +08:00
}
}