> Linux新闻 >

.Net Core下TDengine的ADO.Net EFCore提供程序 1.0.13 已发布

Maikebing.Data.Taos 是一个基于TDengine 的RESTful Connector构建的ADO.Net 提供程序。 它这将允许你通过.Net Core 访问TDengine数据库。Maikebing.EntityFrameworkCore.Taos 是一个TDengine 的 Entity Framework Core 的提供程序,用于你在使用EFCore访问TDengine,由于官方没有提供任何已编译可供Linux、Windows、MacOS的C++ Connector,要实现.Net Core 横跨三平台需要研读C++ Connector代码并在三个平台下进行测试。 与此同时又需要阅读ADO.Net 和EFCore 框架的代码, 压力巨大,因此我先使用 RESTful Connector 等官方发布三平台下经过测试的动态库后再重写ADO.Net,或者当我有时间拜读TDengine代码时可以考虑重写。 

Maikebing.EntityFrameworkCore.Taos 1.0.13 现在已经发布,对Entity Framework Core  的实现已经完成, 另外, 你需要注意的是 , 这是一个时序数据库,不是关系型数据库,有很多关系型需求是会报错的,比如,按照官方要求 第一个字段必须是日期。

下面是最新版本的示例:

///Specify the name of the database
    string database = "db_" + DateTime.Now.ToString("yyyyMMddHHmmss");
    var builder = new TaosConnectionStringBuilder()
    {
        DataSource = "http://td.gitclub.cn/rest/sql",
        DataBase = database,
        Username = "root",
        Password = "taosdata"
    };
    //Example for ADO.Net 
    using (var connection = new TaosConnection(builder.ConnectionString))
    {
        connection.Open();
        Console.WriteLine("create {0} {1}", database, connection.CreateCommand($"create database {database};").ExecuteNonQuery());
        Console.WriteLine("create table t {0} {1}", database, connection.CreateCommand($"create table {database}.t (ts timestamp, cdata int);").ExecuteNonQuery());
        Console.WriteLine("insert into t values  {0}  ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.ToString("yyyy-MM-dd HH🇲🇲ss.ms")}', 10);").ExecuteNonQuery());
        Console.WriteLine("insert into t values  {0} ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd HH🇲🇲ss.ms")}', 20);").ExecuteNonQuery());
        var cmd_select = connection.CreateCommand();
        cmd_select.CommandText = $"select * from {database}.t";
        var reader = cmd_select.ExecuteReader();
        Console.WriteLine(cmd_select.CommandText);
        Console.WriteLine("");
        ConsoleTableBuilder.From(reader.ToDataTable()).WithFormat(ConsoleTableBuilderFormat.MarkDown).ExportAndWriteLine();
        Console.WriteLine("");
        Console.WriteLine("DROP TABLE  {0} {1}", database, connection.CreateCommand($"DROP TABLE  {database}.t;").ExecuteNonQuery());
        Console.WriteLine("DROP DATABASE {0} {1}", database, connection.CreateCommand($"DROP DATABASE   {database};").ExecuteNonQuery());
        connection.Close();
    }
    //Example for  Entity Framework Core  
    using (var context = new TaosContext(new DbContextOptionsBuilder()
                                            .UseTaos(builder.ConnectionString).Options))
    {
        Console.WriteLine("EnsureCreated");
        context.Database.EnsureCreated();
        for (int i = 0; i < 10; i++)
        {
            var rd = new Random();
            context.sensor.Add(new sensor() { ts = DateTime.Now.AddMilliseconds(i), degree = rd.NextDouble(), pm25 = rd.Next(0, 1000) });
        }
        Console.WriteLine("Saveing");
        context.SaveChanges();
        Console.WriteLine("");
        Console.WriteLine("from s in context.sensor where s.pm25 > 0 select s ");
        Console.WriteLine("");
        var f = from s in context.sensor where s.pm25 > 0 select s;
        var ary = f.ToArray();
        ConsoleTableBuilder.From(ary.ToList()).WithFormat(ConsoleTableBuilderFormat.MarkDown).ExportAndWriteLine();
        context.Database.EnsureDeleted();
    }
    Console.WriteLine("");
    Console.WriteLine("Pass any key to exit....");
    Console.ReadKey();

欲进一步了解 请前往 https://gitee.com/maikebing/Maikebing.EntityFrameworkCore.Taos

 

(责任编辑:IT)